• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to run a java app without typing "java "

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...).
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://man.he.net/man8/update-binfmts contains - surprise - a java-example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic