• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to run a java app without typing "java "

 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...).
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://man.he.net/man8/update-binfmts contains - surprise - a java-example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic