Two Laptop Bag
The moose likes Linux / UNIX and the fly likes how to run a java app without typing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Reply Bookmark "how to run a java app without typing "java "" Watch "how to run a java app without typing "java "" New topic
Author

how to run a java app without typing "java "

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9956
    
    6

Many Unix programs let you put in a "#! " that state what should be used to run the program. for example,

#!/usr/local/bin/perl

tell Unix that the file should be run using the perl interpreter found in that path.

Is there something equivalent for Java? I think the idea is that for most other programs/scripts, the user doesn't need to know what the command is to run the program, they can just type "myScript.pl" and Unix knows to use Perl.

Can you do the same thing for java programs?


Never ascribe to malice that which can be adequately explained by stupidity.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5543

Yeah, but those are scripting languages, they run text files. Java is a compiled language, and you cannot prepend "#! java" to a binary. Your choise are create a shell script to run the java app (this is done by most software) or write a C shell to load the JVM and Java app (check out the JNI docs for how to do this).

You can also configure your file manager to execute JAR files using java so that when you double-click them the Java app runs. Of course, the JAR needs to specify the starting class in the manifest.

One other thought - Groovy is a scripting language. And I think that somewhere in the Groovy docs is a way to add the "#! " to the start of the groovy script. And since Groovy is an extension of Java, that could help you accomplish your goal.


JBoss In Action
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9956
    
    6

They are scripting languages, but you can run a compiled 'c' program by simply typing it's name. granted, it is compiled to the native machine language...

have no idea if this is possible, or even if it's a big deal. I'm just kind of playing around.

Thanks for the suggestions.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5543

but you can run a compiled 'c' program by simply typing it's name.

Yes, that is because the C compiler and linker generate binary files, with a very specific layout, containing machine code. Such binary files contain a hex marker (0x7F454C46) in the first several bytes that identifies the file as a "binary executable" and Linux knows how to load and run such a file.

You could always modify the app loader of the Linux kernel (or whatever code handles this in Linux) to recognize the binary marker for a class file (0xCAFEBABE) as a Java file and have it launch the file with Java. Of course you would have to deal with the classpath somehow.

Doing the same for JARs would be trickier because they use the same binary marker as ZIP files (0x504B) but not all ZIP files are "runnable".
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

There is a way to do this in Linux. Maybe it's even enabled by default on Ubuntu. Sorry, I don't know any details, but there is a way to configure different kinds of executables, and you can specify with which programs they should be started. Look around in the /etc directory if you can find anything that has to do with this. (I don't have a working Ubuntu at hand to check it out...).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

http://man.he.net/man8/update-binfmts contains - surprise - a java-example.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to run a java app without typing "java "
 
Similar Threads
What Are Web Logs
Where do I enter environment parms ?
how to set java env variables
How to make a java program run always
arguments of System.exit()