I have created a java application and packed it into a jar file on a Windows platform. Now I wants to deploy that jar file on Debian Linux.
Will the same jar file work for Debian Linux?
Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
i.e.
java -jvm "MyJar.jar"
Will the above command work for Debian Linux?
Generally, it should. However this depends on a few conditions:
If you use native code (JNI) you must make sure that the native library is available for the target platform
You must make sure you have no paths hardcoded which are Windows specific (in fact you should even watch out for special characters like the Path seperator : vs. ;)
You cannot use Runtime specific code
Yes.
Jar files are portable across platforms, and the syntax of the jar command is the same on both Linux and Windows.
EDIT: You should use the latest version of Sun Java unless there is a very good reason not to. Installation instructions: http://wiki.debian.org/Java/Sun
1. Will the same jar file work for Debian Linux?
Yes. Hence the nature of Java (portable code)
2. Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
java -jar "MyJar.jar"
yes, the main idea of java is that it (should) run on different operating systems, as long as a java runtime is installed.
though i have never heard of the -jvm flag.
if you want to start a jar file you should use the -jar flag.
java -jar "MyJar.jar"
you can also read up on the Write once run anywhere principle.
I do my development on a mac but run in linux and windows environments without any problem. Key is not to use JNI, As everyone else have mentioned I would use java -jar "MyJar.jar"
Almost. Use:
java -jar "MyJar.jar"
And of course you shouldn't have used anything such JNI or runtime stuff
Yes, although you might want to do, in Linux:
java -jar YourJar.jar
Instead of:
java -jvm YourJar.jar
Related
I need to execute a jar file on HP-UX that I am not supposed to modify.
I unpacked it using jd-gui and found out that I am failing cause in java there is a condition to check the os, leading to different directions for win, macos, freebds, openbds, gnu and so on.
I am quite sure everything would work if I would be able to make my unix command line reply freebds or openbds to the java call
System.getProperty("os.name")
once executed from a jar file like:
java -jar myjar.jar
is there a way to achieve this? some kind of compatibility mode or a way to preset that parameter.
You can use the -D switch to specify system properties. In my experiment this (unexpectedly) even worked with pre-defined ones like os.name. Therefore this should work:
java -Dos.name=linux -jar myjar.jar
I have tried the following:
in terminal it works
In Intellij it works
I have tried to launch it with javaw.exe but nothing changes
Are there any other options?
This looks like an OS problem that an application building one -- because you said that it works in your IDE and terminal.
Make sure that you installed Java properly in your machine.
In Windows/MacOs, after installing Java, the *.jar files are automatically associated with the java -jar command and makes it runnable via double-click.
In linux, this varies on the flavour or DE you are using. But there's probably a utility in your OS to open *.jar files using java -jar command.
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 from 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.
I'm using a bat file to run my jar. The code in my bat file is this :
#echo off
java -cp analyser.jar be.model.Start
pause
This works fine for windows.
But it doesn't do anything at linux. I also need to be sure it will run on Mac
Bat files are specific to Windows. You would need to execute the command in Linux and Mac in a manner that is specific to those platforms. The actual java call should work the same, I believe. The one change to the java line would be if you had multiple items in the classpath. In that case, you would need to use a colon as a separator instead of a semicolon (which is what Windows uses). (Thanks to khachik for that tip)
For Linux, you would use Shell programming using a BASH script. Here is a link that will describe what you need to do:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
For Mac, you would probably use an AppleScript. Here is an article on how to get started with AppleScripts:
http://www.macosxautomation.com/applescript/firsttutorial/index.html
For Linux, why not use a .sh (shell) file?
As Biggs~ alreay said, the actual Java call should remain the same.
Update:
You will also have to make it executable by changing it's user permissions. To do this, use: chmod +x thescript.sh
Is it possible to execute a JAR file on any OS (like Windows, Linux, Mac OS X)? I want to build a simple application that I want to run on Linux, Windows, and Mac OS X. Could the JAR file be run on any OS with java installed?
The Jar files run on any OS for which a JVM exists.
Yes, as long as you don't use any native libraries (JNI) this is how java works. It's platform independent.
As other said, as long as you have Java installed and avoid using native code, you should be good to go. One thing to note is that you can usually run a JAR file just by double clicking it, and it opens like a native executable (on Windows this is how it works by default, on other OSes you can configure this behavior).
Such JAR files are called executable JAR files. If what you want to create is an executable JAR file, then you need to add a manifest file that tells the Java virtual machine (JVM) the name of the main class. Executable JAR files also can be run on the command line by doing:
java -jar myprogram.jar
If your JAR is not an executable JAR, then to run your program you have to add the JAR to your classpath and then execute the main class. To add a JAR to the classpath:
java -classpath path/to/your/program.jar com.mypackage.Main
Jar files are designed to run on any OS that has a JVM of a compatible version installed. Some jar files, however, may have be compiled from Java code that used OS-specific code (say talking to Windows registries), so testing it on other OS's is wise.
Yes, it can as long as it's not ruining from the terminal or command prompt (like java -jar name.jar.) it should work just fine.