# [java] Running jar from command line like normal exe

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't know if its possible, but can a jar be set up to run like a normal executable at the command line? Like dir, ls, tar, gzip, etc... So that the command line doesn't have to be built with the first java XXXClass So: %> java XXXClass becomes %> XXXClass Is this possible outside of creating a .sh or .bat file that forwards the correct parameters. More or less a wrapper around the command: java XXXClass <params> Is this possible to accomplish for a jar? If so how? L- Thanks for your help

##### Share on other sites
Thats not the best of solutions unfortunately. A lot of modern compression utilities automatically bind the JAR extension so if you double click you end up opening the jar in winRAR or winzip or whatever.

Best way is to use a wrapper around your program, check out the replies (including mine :)) about 8 or so threads down the page, subject "How to bundle JVM with your program?"

D.

-edit- Although problem above only applies obviously when you double click an executable jar :) -edit-

##### Share on other sites
The best way is to make your JAR files being able to start by double-clicking is to install JRE, and the installation program will do it for you. If you don't want to do it, then I can only offer you how I fixed this by myself:
1) Under Windows XP
You open a random folder and go to Tool -> Folder Options -> File types, check for JAR, if there isn't any you create one with New, if there is then you open it with Advanced. Create/edit the Open action to look like this:
 "C:\java\bin\java.exe" -jar "%1"
. It is important to put those " " cause otherwise the whole thing won't work.

2) Under Linux
Under KDE I had to specify a file association by going to System -> Configuration -> KDE -> Components -> File Associations (maybe not Components, I'm not sure now) and there you associate JAR with
 /sbin/java -jar %u

To open .class files, both under Windows and Linux I had to write a program of my own. Because won't work if you include the .class extension, I made a program which takes the file name as its first arguement, chops off the .class part and executes the proper java command.

##### Share on other sites
I'm sorry maybe I was unclear... what I'm after is not to double click the jar file, though that would be optional too. What I would like is to open a command prompt and just type the file name and have it run like a normal executable without:

%>java -jar

prefix.

Is this possible?

L-

##### Share on other sites
You mean ./myapp.jar
Well under Linux I guess you could make a process that listens to what is written in the shell, and when the word "jar" occurs it checks if its a JAR file, and if it is then it executes the proper command. I'm assuming of course you know how to use fork() and pipe(), in which case it shouldn't be too hard to write such a program.
You would have to create a process which starts bash, and sets its stdin to a pipe that is created in your program. When the user types in something, it first goes through your program, where it checks stuff, and then if its not a JAR you send it to bash. If it is a JAR it does not let the command reach bash, but instead you start it yourself.

##### Share on other sites
No need :) Linux has it as a feature in the kernel sources IIRC - you just compile your kernel with a small extension, and it is able to "directly execute" a jar file or class file just as you want :).

Google around linux, java, and binaries (possibly even things like ELF too...)

redmilamber

##### Share on other sites
The simple answer is "no". You can't run a Java archive directly in exactly the same way as you can't run a DLL/SO etc. directly.

You'll want to write a wrapper for your archive; if you know your environment, a simple hard-coded BAT or shell script will work fine. Otherwise, Google for Java packaging and distribution tools.

--cfmdobbie

##### Share on other sites
Well, actually, Linux does have kernel support for Java binaries. If you enable this in your kernel you can set the executable bit on a class file and just run it like this:
box\$ ./MyCoolApp.class

You are asking about a JAR-file, however, which will probably be a bit trickier. A quick browse through the file Documentation/java.txt led me to believe that it IS actually possible, but more careful reading should tell you.
(My docs are saying something about a wrapper script to hande broken file name handling, but I currently only have access to an older kernel, 2.6.5).

##### Share on other sites
For windows you should try JSmooth.

There is another page with interesting links:
http://lopica.sourceforge.net/installer.html

For linux you can try jstub from here

Edit:

• 10
• 16
• 14
• 18
• 15