开发者

creating 100% standalone executable jar that doesn't require the java command

开发者 https://www.devze.com 2023-03-20 23:46 出处:网络
so apparently if you create an executable jar, in order to run it you still need the java command: java -jar something.jar

so apparently if you create an executable jar, in order to run it you still need the java command:

java -jar something.jar

but what if I just want it to run without the java command, so just directly f开发者_如何学Gorom the command line

something.jar 

is there a way to export my java app in eclipse in order to accomplish such


On Unix systems you can append the jar file at the end of an executable script.

On Windows you have to create a batch file.

For instance in Unix:

$cat HelloWorld.java  
public class HelloWorld {
    public static void main( String ... args ) {
         System.out.println("Hola mundo!");
    }
}
$cat M.mf 
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$@"
$javac HelloWorld.java  
$jar -cmf M.mf  hello.jar HelloWorld.class 
$cat hello.jar >> hello 
$chmod +x hello
$./hello  
Hola mundo!

In windows you have to create a batch file like:

::hello.cmd 
javaw -jar hello.jar 

Which has the same effect.

On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.

I hope this help


Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.

I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.


Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.

For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.


What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.

The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net

Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.


The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.

To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).

There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.

If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++


Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号