Sign in to follow this  
Lucidquiet

[java] Running jar from command line like normal exe

Recommended Posts

Lucidquiet    199
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 this post


Link to post
Share on other sites
daireq    236
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 this post


Link to post
Share on other sites
gcsaba2    217
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 this post


Link to post
Share on other sites
Lucidquiet    199
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 this post


Link to post
Share on other sites
gcsaba2    217
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The simple answer is "no". You can't run a Java archive directly in [I]exactly[/I] 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 this post


Link to post
Share on other sites
DaBono    1496
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 this post


Link to post
Share on other sites
Lucidquiet    199
Thanks for the above posts -- all of them.

Since I have to use both Linux, and Window, all of the above information is usefull. (I didn't know about the linux kernal being versatile in that way) And the JSmooth looks really useful aswell.

Thanks,
L-

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this