Running a JAVA project with external jars folder - java

I'm trying to execute my JAVA app on a Linux OS where the necessary jars are located in a different folder. How can I execute my project using the external jars?
project location:
$ pwd
/root/MyApp/bin
$ ls
Deletion.class
jars location:
/opt/jars/*.jar
My failed execution:
$ java Deletion
... NoClassDefFoundError ...
$ java -cp "/opt/jars/*.jar" Deletion
Error: Could not find or load main class Deletion

When setting the class path with -cp ..., you have to also specify the current working directory (as this is not part anymore):
java -cp ".:/opt/jars/*.jar" Deletion

java -cp "location of jar file:." Deletion

Related

Unable to run Java main class though windows commandline

I have an external jar and I have written 2 classes named 'TestConnector.java' and 'APIExampleUsageTestData.java' and I have placed these 2 files under default package.
I have placed all the 3 artifacts -> externalConnector.jar, TestConnector.java and APIExampleUsageTestData.java into a folder named 'temp'.
I have opened commandline in the temp folder context and compiled the class files with below command and it executed without any errors:
javac -cp "externalConnector.jar" *.java
I can see that there are 2 class files present in the tem folder.
There is a main method in the APIExampleUsageTestData.class and I'm trying to call that class with the below command, but, is failing
When running the below command in 'C:\Users\AppData\Local\Temp\0.15429262750877082' folder context
java -cp "externalConnector.jar" APIExampleUsageTestData
I'm getting error Error: Could not find or load main class APIExampleUsageTestData
When I trying to run the command, without any class path entry to check what error it is throwing:
java APIExampleUsageTestData
It throws error: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException
It is a Windows Operating System. Could you please let me know how to run the program successfully?
You need both the current directory and the jar file to be on the classpath. So you want (Linux / Mac):
java -cp .:externalConnector.jar APIExampleUsageTestData
Or (Windows)
java -cp .;externalConnector.jar APIExampleUsageTestData

Trying to build/run java program from cmd line with external library

I am trying to run a java program from the cmd line but I have an external library, as per below:
I have tried the advice from this previous question (Java - Build and run eclipse project from command line), like this:
java -cp <classpath> <main class> <args>
In my case (with response):
java -cp "ejml-v0.34-libs/*" MatrixServer
Error: Could not find or load main class MatrixServer.java
I may be confused as how to run my file and the path to it?
When I try and run the java file normally with javac then java, it has issues referencing the external 'ejml' library. I'm just looking for an easy way to run it form the cmd line!
java -cp "ejml-v0.34-libs/*;." MatrixServer
. to include your current directory(where compiled files are located ) along with ejml-v0.34-libs folder.
<classpath> - list of directories and/or JAR-files where needed classes reside separated by ";" for Windows or ":" for linux (default classpath is "." - the current directory);

Unable to execute java program in jenkins?

I am trying to execute simple java class through jenkins and i have followed below post but no results,please help how to execute complete java program in jenkins.
How to compile and run a simple java file in jenkins on Windows
Always getting error as
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Run Java Class
[Run Java Class] $ cmd /c call C:\Windows\TEMP\hudson7739288570136427109.bat
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>javac "E:\Java_Tutorials\JavaPractices\src\com\practice\javsSuperCars.java"
javac: file not found: E:\Java_Tutorials\JavaPractices\src\com\practice\javsSuperCars.java
Usage: javac <options> <source files>
use -help for a list of possible options
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>java SuperCars
Error: Could not find or load main class SuperCars
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
The post that you referred was a sample example where the Java class that was executed was on the Jenkins Workspace folder directly, but your application is different as it is with a project folder structure.
Jenkins doesn't identify your Main class without you adding it to the CLASSPATH implicitly. Here the necessity of setting the CLASSPATH is more important.
In the Execute Windows batch Command section, you need to traverse to your Main class and then you need to issue the command to compile the same.
Hope this helps!

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.

Could not find or load main class when I add external libraries

Hey I got this problem when running a java program through my cmd.
I use to external libraries : lwjgl.jar and slick-util.jar
when I compile
javac -d bin -sourcepath src -cp ;lib/lwjgl.jar;lib/slick-util.jar src/*.java
everything works fine
but when I try to run it
java -cp bin .;lib/lwjgl.jar;lib/slick-util.jar; Game
I get this error
Error: could not find or load main class .;lib.lwjgl.jar;lib.slick-util.jar;
Replace the space in the run command classpath with a classpath separator
java -cp bin;.;lib/lwjgl.jar;lib/slick-util.jar; Game
^
as an improvement to the run command you could use a classpath wilcards, for example
java -cp ".;bin;lib/*" Game
It is important to use quotes under Windows for this option

Categories