How can I compile/package a Java .jar as a Mac OS X command-line tool?
I'm not a Java programmer, but I often use a Java program someone else wrote, that's currently packaged as a .jar file, that I invoke from the Ma开发者_JAVA百科c OS X Terminal as:
$ java -jar /path/to/jarfile/myJavaCommand.jar -some other arguments ...
I'd much rather be able to invoke it like any other command-line tool:
$ myJavaCommand -some other arguments ...
I have the .java sources and a Makefile that turns them into the .class files and packages them into the .jar.
I have found documentation for turning Java programs into Mac OS X double-clickable ".app" bundles, but I don't think that's what I want because I want a single executable file that I can drop into, say, /usr/local/bin
so I can easily invoke it from the Terminal, with varying arguments (and without typing in long paths).
I don't want to solve this with a separate shell script or shell alias because I move this tool around from machine to machine, and prefer not to have to copy around a shell script or modify the shell environment on the other machines.
So is there a way to compile/package a Java program as a single-file Mac OS X command-line tool?
Unfortunately, you can't. The shell script approach is pretty much it.
Your java code is not natively executable, it is executed by the JVM. In addition, the jar file is actually a zip archive ... so the shell simply has no idea what to do with it what so ever.
Look at YAJSW, which wraps Java apps for easier launching on native platforms.
You'll still have a big directory of stuff to copy around; I don't know if it will solve your specific questions on OS X, but we use it for Windows launching and it works well.
精彩评论