java.lang.NoClassDefFoundError when running my java program - java

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

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!

Class not found when running program from command line

Here is the structure of my project:
MyProject
-lib
-mp3agic-0.9.0.jar
-src
-com.company
-Main.java
Inside Main, I import the content of mp3agic:
import com.mpatric.mp3agic.*;
From MyProject, I compile the class using:
javac -cp "lib/*" src/com/company/Main.java
From MyProject/src, I try to run the program with:
java -cp . com/company/Main
and I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mpatric/mp3agic/InvalidDataException
Apparently, the jdk can not find the dependencies from the lib folder when running the class, albeit this problem doesn't occur when compiling.
I guess there's a problem with the syntax of the command, but I just can't find it.
Can you please help me?
EDIT: I solved the problem, moving the lib folder under src and then running:
C:\MyProject\src>javac -cp "lib/*" com/company/Main.java
C:\MyProject\src>java -cp .;"lib/*" com/company/Main
Now, the code compiles and executes without any issue.
HOWEVER, if I leave lib under the project root, this is what happens:
C:\MyProject>javac -cp "lib/*" src/com/company/Main.java
C:\MyProject>java -cp .;"lib/*" src/com/company/Main
Error: Could not find or load main class src.com.company.Main
WHY?
You should use maven or gradle or you'll have to say which console are your using, and which operating system -if you don't know what a console is, you'll first have to google for that before messing with javac directly- But if you insist:
for Linux, OSX, any UNIX-like:
javac -cp ".:lib/mp3agic-0.9.0.jar" src/com/company/Main.java
for Windows:
javac -cp ".;lib/mp3agic-0.9.0.jar" src/com/company/Main.java
To run under windows:
java -cp .;lib\mp4agic-0.9.0.jar com.company.Main
You may need to use slash (/) instead of backslash depending of which console you're using. Or just try "/" or "\" and see what works for you.
If you still get class not found exception, then the issue is not related with classpath or java command but on your code (hard to say, without seeing the code).
java.exe -classpath {path} Main
I think you have to add classpath also and when run the program(not only compile)

How to import external jars in command line or Atom?

I have been using IntelliJ to run my java programs that require some external jars. I also learned that if I want to compile and run my program from the command line I should do the following:
java -classpath someJar.jar YourMainClass
or for many libraries:
java -classpath someJar.jar;myJar.jar YourMainClass
However, while placed in the src folder where my class it doesn't seem to find my class.
I also like using the Atom text editor but I don't know any package that can import external libraries like an IDE does. So how do I do it in Atom or in cmd in Windows 10? I am kind of a newbie to java dev outisde my beloved IDE, so I would really appreciate some help.
If you're on a Windows system then try compiling (with the jar) using the following command in cmd:
javac -cp .;jar-name.jar *.java
To run the command with the jar use:
java -cp .;jar-name.jar JavaCodeName
If you're on a Unix system then you can try the following to compile in terminal:
javac -cp jar-name.jar:. *.java
And to run it use:
java -cp jar-name.jar:. JavaCodeName
I'm not too familiar with Atom so I don't know if there is an attachment to do this, but it should work for terminal / command prompt.

How to run java code from shell script on linux

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 :.

Cannot find class in classpath

I've been reading up on this error but am now confused. I'm trying to run my java test file from the win7 cmd line but am getting that dreaded error cannot find class in classpath.
The script runs fine as a testNG file inside the Eclipse IDE but not from the cmd line.
My classpath dump:
C:\lib\selenium-java-2.37.0.jar;
C:\lib\selenium-server-standalone-2.37.0.jar;
C:\EclipseIDE\ProteinBar\bin;
C:\EclipseIDE\ProteinBar\src\com\proteinbar\staging;
C:\TestNG;
My cmd line test string:
java -cp C:\lib\*;C:\EclipseIDE\ProteinBar\bin org.testng.TestNG DeleteBillingAddress.xml
Thanks for any help...
it doesn't appear that the testng jars are on your classpath. I'm guessing they live under c:/testng so you'll want to add something to your -cp reflecting that.
You need to specify the jars individually, e.g.,
java -cp C:\lib\selenium-java-2.37.0.jar;C:\lib\selenium-server-standalone-2.37.0.jar;...

Categories