noclassdeffound error in java, linux - java

running a java class by shell script:
java -cp $CLASSPATH CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
while running the script in unix, getting error,
Exception in thread "main" java.lang.NoClassDefFoundError: CG_GpsRequest
Caused by: java.lang.ClassNotFoundException:
CG_GpsRequest is the class file name.

Okay.. If I understand you correctly..
I think its problem with the Package name specification..
Your calss CG_GpsRequest you must have specified in Packaging way for example
com.xxx.yyy.CG_GpsRequest
So I think JVM is searching your class com.xxx.yyy.CG_GpsRequest in this patter so Please run your script with the following modifications.
java -cp $CLASSPATH com.xxx.yyy.CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
This is just assumption that you have created your class in Pacakgin hirarechy since you havent specified more information.

Add the folder/path in which your java class exists to your CLASSPATH

Related

Execute certain java class which is not main class of a jar file from linux

I need to execute a java class in a jar file. I tried as follows:
java -jar MyJar.jar
And got: no main manifest attribute, in MyJar.jar.
Then I realize that I must run a certain class which is in /directoryInsideJar/MyClass.class, but I don't have permission to edit MANIFEST.MF file. I found that I directly could theoretically run it by:
java -cp MyJar.jar firstdirectoryinpath.path.MyClass
But I got:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq (...)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
(...)
I'm pretty sure that the class exists and the path is correct.
What I'm doing wrong?

how to run a java file from a parent directory?

I am able to compile a java file that is inside a directory by using javac <directory_name>/Solution.java.
but I am not able to run the java program in the same manner ie. java /Solution.
as it gives me this error :
Error: Could not find or load main class code.Solution Caused by:
java.lang.NoClassDefFoundError: Solution (wrong name: code/Solution)
Here code is my directory name.
I could cd into the directory and do it. which actually works but that is not the behavior that I desire.
How to achieve this?

Java class not found in Jackson

I'm new to Java and have a program that's throwing the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
at ExtractCustomerIds.main(ExtractCustomerIds.java:21)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.map.ObjectMapper
at jdk.internal.loader.BuiltinClassLoader.loadClass(java.base#9-internal/BuiltinClassLoader.java:366)
at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.base#9-internal/ClassLoaders.java:184)
at java.lang.ClassLoader.loadClass(java.base#9-internal/ClassLoader.java:419)
... 1 more
I know the problem has to be with paths, classpath or jar name, but I can't figure out as of now.
My folder structure is:
.
- get_customer_ids
- ExtractCustomerIds.java
- libs
- jackson-all-1.9.11.jar
So when I'm inside the get_customer_ids I give the following command:
get_customer_ids$ javac -cp ../libs/* ExtractCustomerIds.java and this causes the error above.
I thought I was setting the classpath correctly, but it's still not working. What's missing?
The exception occurs when running your program - you are specifying the classpath while compiling. The classpath set during compilation is not stored inside the compiled class files or the like - you need to correctly specify it when running your program, too!
Like so:
$ java -cp ../libs/* ExtractCustomerIds

Why adding class path caused main class not found?

I get a NoClassDefFoundError when trying to run a Java class without providing the proper class path. However when added the needed class path, java complaints it cannot find the main method. If you have any idea of what's happening here, please point me in the right direction. Thank you
$ java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix1D
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
... 6 more
$ java -cp resources/colt.jar MyClass
Error: Could not find or load main class MyClass
Assuming MyClass isn't in a package, you need something like (on Windows)
java -cp resources/colt.jar;. MyClass
or (otherwise)
java -cp resources/colt.jar:. MyClass
To also include the current directory. Alternatively, you can set the CLASSPATH environment variable.
On Windows,
set "CLASSPATH=resources/colt.jar;."
otherwise something like (depending on your shell)
export CLASSPATH="resources/colt.jar:."
then
java MyClass
try to include the current directory in the classpath as well. usually we add new jars to classpath liek this:
java -cp %CLASSPATH%;resource/colt.jar MyClass
or on Linux as:
java -cp $CLASSPATH:resource/colt.jar MyClass
Additionally u can also add .
i.e the currrent directory to the classpath.

exception in thread 'main' java.lang.NoClassDefFoundError

When I try to execute the following program from DOS I get the results below..
The following program is in C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src\java1
package java1;
public class MyProgram{
public static void main(String[] args){
System.out.println("Rome wasn’t burned in a day!");
}
}
javac MyProgram.java
works fine
But java MyProgram gives the following:
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
: java1/MyProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
................
Why this is happening??
My environment variables:
CLASSPATH: .;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_05\bin
PATH: C:\Program Files\Java\jdk1.7.0_05\bin
JAVA_HOME: C:\Program Files\Java\jdk1.7.0_05
I have seen similar problems here but i cannot find the solution to my problem.
Thank you in advance!
This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Refer this Link
2 points you should keep in mind when using java tool:
Add the class to the classpath.
Use the fully qualified name of the class to be run.
Hence:
java -cp C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin java1.MyProgram
assuming the following file exists after compilation:
C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin\java1\MyProgram.class
For more info, see:
Mastering the Java CLASSPATH.
"Setting the class path" Documentation.
NoClassDefFoundError in Java comes when Java Virtual Machine is not
able to find a particular class at runtime which was available during
compile time. For example if we have a method call from a class or
accessing any static member of a Class and that class is not available
during run-time then JVM will throw NoClassDefFoundError.
Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why it’s not available in Classpath if we are expecting it to be. There could be multiple reasons like:
Class is not available in Java Classpath.
You might be running your program using jar command and class was
not defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
Try in this way
run command prompt as Administrator, and
cd C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
then
javac java1/MyProgram.java
Then
java java1.MyProgram
This will work.
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
This exception is thrown when the JVM cannot finf your class at run time
From C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
execute " "java java1.MyProgram" –

Categories

Resources