How to run java code from shell script on linux - java

I want to set a cron job for my java code and I have tried this from project directory
java -classpath .:/home/project/lib/* pkg_name.my_code
and it works fine, however I dont know how to run it from any other directory[like in script]
I have tried to add diroctry (having compiled classes) in classpath like this
java -classpath .:/home/project/lib/*;/home/project/pkg_name/* pkg_name.my_code
also tried
java -classpath ".:/home/project/lib/*;/home/project/pkg_name/*" pkg_name.my_code
but it gives error:
**Error: Could not find or load main class pkg_name.my_code
**
can any please help me ?

If you want to run your project from another directory, then you need to include your project in classpath. So you can do this
java -classpath ".:/home/project/lib/*:/home/project" pkg_name.my_code
For example :
java -classpath ".:/home/test/runjavafromanotherdirectory/lib/*:./runjavafromanotherdirectory" com.test.Main
One of your mistake is you are using ; instead of :.

Related

Bash on windows can't find class even after setting classpath

I need to include .jar file when running the java class. When I use cmd on my windows, I can successfully run using the following command
java -cp .;MyLib.jar; MyClass
However when I tried to use bash on the same PC, I got error - can't find or load main class. I searched online and some say I needed to use ./* instead of just . for the current directory. So far I tried these commands:
java -cp .:MyLib.jar: MyClass
java -cp ./*:MyLib.jar: MyClass
java -cp ".:MyLib.jar:" MyClass
java -cp "./*:MyLib.jar:" MyClass
only getting "ClassNotFoundException" every time.I know the class exists because if I switch to cmd when it runs fine. I was also able to run the class on bash shell on a mac using the following command
java -cp .:MyLib.jar: MyClass
Then I built a simple class and ran in bash without classpath
java MyTest
So using -classpath caused the issue. How do I include the current directory in bash? Why did it work on bash on a mac? Is it because I was running bash on windows? I am so puzzled by this problem.
And if it helps, I can compile in bash just not able to run the class. javac -cp MyLib.jar MyClass.java compiled the file in bash. I am working under the same directory where MyClass.class is.
Edit: the solution is java -cp ".;MyLib.jar;" MyClass. I thought I posted here for anyone who ran into the same issue as I did. Please see comments for explanation. Thank you torek!

Java don't run the main file (classpath)

Hello guys i'm new with java and i'm starting to work with packages etc, i have a problem when i try to run my main file located at
C:\Users\Robert\Desktop\Java\latebd\Test.Java
When i run
java -cp C:\Users\Robert\Desktop\Java\latebd\latebd.Test
instead of having the code running i get this (it's in italian bu basically it's telling all the options when i use java -
Why do i get that?
you need to compile it e.g. javac C:\Users\Robert\Desktop\Java\latebd\*
provide the path where it's already compiled as a classpath, and to run it use java -cp "C:\Users\Robert\Desktop\Java\latebd\*" latebd.Test
Try java -cp "C:\Users\Robert\Desktop\Java\latebd\latebd.Test"

running jar file with dependencies : java could not find or load the main class OSMImportTool

I'm trying to run a jar file from a Windows command prompt. From what i can see all the code and dependencies are there. The code given to run this is :
java -Xms1280m -Xmx1280m -cp
"target/osm-0.2.2-neo4j-3.5.1.jar:target/dependency/*"
org.neo4j.gis.osm.OSMImportTool --skip-duplicate-nodes --delete --into
target/databases/map2 samples/map2.osm.bz2
The error returned is:
java could not find or load the main class
org.neo4j.gis.osm.OSMImportTool.
Have tried playing around with the path but no luck. Any suggestions how to make this work would be great.
Thanks
java -Xms1280m -Xmx1280m -cp "target/osm-0.2.2-neo4j-3.5.1.jar;target/dependency/*" org.neo4j.gis.osm.OSMImportTool --skip-duplicate-nodes --delete --into target/databases/map2 samples/map2.osm.bz2
replaced the colon with a semi colon

java.lang.NoClassDefFoundError when running my java program

i'm new with java.
when i come to run my java app i'm getting this error below:
java.lang.NoClassDefFoundError: org/apache/poi/EncryptedDocumentException
the issue it is ALSO happens when i'm adding the classpath to my command:
java -jar myApp.jar -classpath .\lib
BUT,
when i'm copy myApp.jar to the lib direcotry and running form there it's working fine.
thanks for the help guys.
You cannot use both -jar and -classpath at the same time. If you use -jar, it will ignore your -classpath and use settings from the manifest in the jar. Try all in the classpath:
java -cp myApp.jar:lib/*:. mypackage.MyClass
On Windows you need to use ';' instead of ':'
java -cp myApp.jar;lib/*;. mypackage.MyClass
See similar question here: Execute jar file with multiple classpath libraries from command prompt

How to compile and run HelloWorld.java example from Sphinx4 without and IDE in Linux?

I have been testing the examples (HelloWorld.java) from Sphinx4 with Eclipse, but I would like to compile and run them from the command line.
The application needs 5 .jars to be able to run, I have read that in order to compile a java class with multiple .jars I need to execute the following command (as an example I will show short names):
javac -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld.java
The console does not throw any error messages:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > javac -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld.java
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test >
I think that the compilation succeeded. Now I would like to run the application, I read that in order to do this, I have to execute the command as follows (Using short name example as before):
java -cp one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
This is the message that the console throws:
parias001#parias001-pc:~/Projects/citadel_voices/sphinx_test > java -cp jsapi.jar:sphinx4.jar:TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar:WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar:WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar HelloWorld
Error: Could not find or load main class HelloWorld
I don't know what is going on here, I should also say that I do not have a lot of experience using external .jars.
The names of the .jars are:
jsapi.jar
sphinx4.jar
TIDIGITS_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
WSJ_8gau_13dCep_8kHz_31mel_200Hz_3500Hz.jar
WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar
I appreciate any help you can give me.
You have to include current directory in classpath:
java -cp .:one.jar:two.jar:three.jar:four.jar:five.jar HelloWorld
Note the leading .:
From this reference:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Categories