This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 1 year ago.
I'm trying to compile a java file that uses multiple jar files as imports.
the command that I have used to compile my code :
javac -cp jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner.java
as a result two .class files are created : TestRunner.class and TestRunner$1.class
then I run the command :
java TestRunner
but it throws an error that says:
Error: Unable to initialize main class TestRunner Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
I have included all the required libraries in the javac command, tested it with the IDE and it works fine.
I have tried other versions of the jackson library but I'm stuck with the same error.
You need to specify the classpath when running your code, by using the same -cp args that you used when compiling, plus the folder where your compiled class is in.
In your cas that would mean something like java -cp .:jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner
The libs you specified are not included in the .class files generated, so Java still needs them to understand how to call the code that's not coming from your class file.
Related
This question already has answers here:
How to run a java class with a jar in the classpath?
(4 answers)
Closed 7 years ago.
I am unable to execute selenium tests(JUNIT) from command line
my project folder path class file
C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash
File :LoginTest.class
my project folder path java file
C:\Users\CP042756\workspace\BLR_demo1\src\com\analytics\logindash
File:LoginTest.java
jar file folder: C:\jars\imp\selenium-2.45.0\libs
jar fiLe: junit-dep-4.11.jar
it runs properly in Eclipse
i want to run it in command line
i have tried the following commands from the command line
1)
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar:C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash org.junit.runner.JUnitCore LoginTest
Error:Could not find or load main class
2)java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar org.junit.runner.JUnitCore LoginTest
Error:Could not find class:Login test
Exception in thread main java.lang.noclassdefounderror
Please help,
You have to use semicolon as path separator in Windows. Then your first example should work.
For class files there are two different rules. check, which one applies to your situation:
For .class files in an unnamed package, the class path ends with the
directory that contains the .class files. For .class files in a named
package, the class path ends with the directory that contains the
"root" package (the first package in the full package name).
So, for the latter try this:
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar;C:\Users\CP042756\workspace\BLR_demo1\bin org.junit.runner.JUnitCore com.analytics.logindash.LoginTest
I'm trying to run a class that I wrote that is importing the jawin library.
My command line is as below:
java -cp C:\class_path ClassName
I'm getting:
Exception in thread 'main' java.lang.NoClassDefFoundError org/jawin/funcptr
What am I doing wrong?
Doesn't the JAR that I'm adding to the build path get into the binary file that I've compiled?
If not, then when I publish my class file how will it know to find the external JAR? What if it doesn't exist on the target computer?
This question already has an answer here:
How do I compile a java file that has jar dependencies?
(1 answer)
Closed 8 years ago.
Background: Running on an ec2 instance not eclipse.
I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)
The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.
I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.
EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.
Any help is much appreciated. Thank you!
If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.
If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).
Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.
try to Add the path in system variable and restart your computer.
This question already has answers here:
How do I create a ZIP file in Java?
(2 answers)
Closed 9 years ago.
Is it possible to compile class file to jar file not using jar -cvf options.
I want to compile jar files by source code.
Cant somebody show me some example codes
A JAR simply is a ZIP file.
While I am not quite what you are trying to achieve, I can give you these hints:
Compiling: javac Hello.java
Creating a JAR: zip Hello.jar Hello.class
If you want to have a JAR containing your sources, you could as well run the above command with: zip Hello.jar Hello.java
Also note that, if you are using a build tool like for example maven, there are various plugins for suchs tasks such as 'assembly' (for maven).
I'm using a .jar file which I imported to Eclipse, and when I used Eclipse to run the application is works just fine.
But when I try running it from the command line with just the command java ClassName I get a NoClassDefFoundError message.
The .jar file is in the same directory as the main class I need to run.
I tried using the classpath command in the terminal
java -classpath pack.jar ClassName
but I got the same error but this time it was in the main thread
Exception in thread "main" java.lang.NoClassDefFoundError
Why does my program work in Eclipse but not on its own?
I guess you'll need to use: java -classpath .:pack.jar ClassName.
NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime.
Its Not Even Close To Being equal to ClassNotFoundException n u mustn't confuse them together, so u need to include the whoe path of the .jar file,
use the previously explained technique to do so, or if it was a jar file that want use frequently without using the call to java -classpath .. thing try importing the file into the OS class path directory.