Running java jars under linux - java

I created a jar file and want to run it on linux machine.
In win32 machine I'm using:
java -classpath myclass.jar;log4j-1.2.16.jar;mysql-connector-java-5.0.8-bin.jar;. com.name.myClass.MyClass
However, on linux its doesn't work? Any ideas how to do it?

Use : instead of ; to separate items in your classpath:
-classpath myclass.jar:log4j-1.2.16.jar:mysql-connector-java-5.0.8-bin.jar:.

Related

Cannot execute Java Classes on OSX

I'm trying to execute a java build using commandline in OSX 10.10,
The command I'm using:
java -classpath ./bin;./libs/log4j-1.2.17.jar example.hello.Server localhost:80
I've got MAMP up and running on the classPath and java is in the same "classes" directory.
Terminal is showing all options that can be used with java and is not executing the file. The same happends when using -cp.
Thanks
You have a misplaced ; instead of : in your classpath string. Try:
java -classpath ./bin:./libs/log4j-1.2.17.jar example.hello.Server localhost:80
Remember on OSX (or any other Unix flavor system) path separator is : not ; as on Windows/DOS

How to detect java installation from a batch program

I want to run a jar file on a client machine using batch script. But the problem is its not clear where is the java installed. There are 3-4 fixed paths where java.exe can exist.
How to write a batch script to find where java is installed on any of those predetermined locations and then run a command to execute a jar file.
Assume java is not set in the environment variables and no access to windows registry.
On windows :
c:\> for %i in (java.exe) do #echo. %~$PATH:i
On linux:
$ which java

Ubuntu System Start-up: What command to insert to run java app?

In Ubuntu 10.10, System/Preferences/Startup Applications, I am trying to add a .jar program. If the program sits in home/john/this-folder/app.jar, what would I put in the command line for it to run on start-up?
java -jar /home/john/this-folder/app.jar [optional arg if any]
java - the Java application launcher
Note that you need to include jar in classpath if you java app needs dependecies.
You do that by:
java -jar /yourApp.jar -cp /home/zzz/libs/

Jar file of java

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

How do I run a java program from a different directory?

I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

Categories

Resources