3

I would like to use use java to write a tool to use under linux so that I can call it like I call "ls". What do I need to do? I planed to:

  1. write a java with main function take my arguments, do the job. How do I return results? Java main cannot return string.

  2. Where is the best place to add my jar to in the system? /user/bin?

  3. how can I call it with minimum typing? I don't want to type java jar XXXXXX com.mynamespace.myfunction ...... How can i just call it by using a simple name like "ls"?

Thanks a lot

1
  • You might want to consider using groovy as well. Commented Jul 25, 2014 at 18:21

2 Answers 2

7

You can create a bash file /usr/bin/custom_program pointing to your jar:

#!/usr/bin/bash
java -jar /path/to/your/java.jar # add any custom command line switches here

Don't forget to mark it as executable with sudo chmod u+x /usr/bin/custom_program.

Your output would be given as System.out.println() calls in your program -- no need to return String from main.

Then, you can call your java program with custom_program with the help of the bash script above.

Sign up to request clarification or add additional context in comments.

Comments

1

In addition of a shell script, you might also use binfmt_misc tricks.

I prefer the shell script, it is probably more portable (some Linux system might not load, or have available, the kernel module required to support binfmt_misc)

As the kernel's Documentation/java.txt explains:

2) You have to compile BINFMT_MISC either as a module or into the kernel (CONFIG_BINFMT_MISC) and set it up properly. If you choose to compile it as a module, you will have to insert it manually with modprobe/insmod, as kmod cannot easily be supported with binfmt_misc. Read the file 'binfmt_misc.txt' in this directory to know more about the configuration process.

3) Add the following configuration items to binfmt_misc (you should really have read binfmt_misc.txt now): support for Java applications:

 ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:'

support for executable Jar files:

 ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:'

support for Java Applets:

 ':Applet:E::html::/usr/bin/appletviewer:'

or the following, if you want to be more selective:

 ':Applet:M::<!--applet::/usr/bin/appletviewer:'

Of course you have to fix the path names. The path/file names given in this document match the Debian 2.1 system. (i.e. jdk installed in /usr, custom wrappers from this document in /usr/local)

using gcj

Another possibility (which I don't recommend) would be to use gcj (the old Java compiler inside GCC).

gcj enables you to compile a Java program into a native Linux ELF binary executable. But I don't recommend it because:

  • few (and less and less) people inside the GCC community are working on gcj
  • few people are using gcj ; I only met only one occasional developer using it
  • so gcj is becoming obsolete
  • gcj generated code which allocates a lot does not perform very well (because it uses Boehm garbage collector which is much slower than the GC in other Java implementations). However on Java code with a low allocation rate it may run quite fast (because of the powerful optimizations of GCC done in GCC language-neutral middle-end).
  • the Java language supported by gcj is a subset of some quite old Java standard.

1 Comment

It is not only a link, it is a name binfmt_misc so specific that your'll find documentation easily (even when the original link, to wikipedia, is gone)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.