Java classpath issue with external jars - java

I am having issues compiling external jars along with my source code. My .java files and the external jar reside in the same directory. Here is my makefile:
JFLAGS = -g
JC = javac -cp ${CLASSPATH}
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSPATH = *.jar
CLASSES = \
AESCBC.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
The compilation succeeds. However, when I runjava AESCBC, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

You need to specify your dependencies when running your class. Assuming your class is not compiled into a JAR you will run it like so:
java -cp [PLACE YOUR DEPENDENCIES HERE] AESCBC

Related

Using classes from a jar library gives me a strange JNI error

This only happens when I'm using a jar file. I have a MouseTest.java file and a MazeRunnerAssets.jar file in the same directory. I also have .java files in another directory I'm compiling. This is my command using javac:
javac -cp MazeRunnerAssets.jar Maze_DS/MouseTest.java ../src/Maze_DS/*.java
when I do java Maze_DS.MouseTest I get:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: actor/Actor
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: actor.Actor
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
If I remove the code inside of MouseTest.java that is using certain classes inside MazeRunnerAssets.jar then it will run. Both java and javac have the same version. What could be the problem?
My directory structure is:
MazeRunner/
├── src/
│ ├── Maze_DS/
│ ├── other .java files...
├── test/
├── Maze_DS/
├── MouseTest.java
├── MazeRunnerAssets.jar
The directory I'm running the command from is MazeRunner/test.
It says it can't find Actor, which is one of the classes inside the .jar file.
Inside the .jar file there are a list of directories that contain .class files, .png files, a manifest file, etc.:
MazeRunnerAssests.jar:
actor/
-------- Actor.class
asset/
grid/
gui/
META-INF/
world/

ClassNotFoundException Error any ideas?

I am new to Java and I get this error for my code:
C:\Users\Unknown\Desktop\AIB>java GreenhouseControls -f examples1.txt
Restarting system
java.lang.ClassNotFoundException: ThermostatNight
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at GreenhouseControls$Restart.action(GreenhouseControls.java:139)
at tme3.Controller.run(Controller.java:32)
at GreenhouseControls.main(GreenhouseControls.java:197)
I believe I have a classpath errors.
Did you try to add your compiled java files (i.e., Event.class, ThermostatNight.class, ...) of Event.java, ThermostatNight.java to the classpath:
java -cp "target/*" GreenhouseControls -f examples1.txt
I assume that your compiled class files are in the target directory.
I solved in this way.
java -classpath YourProjectWar.jar com.yourpackage.ClassWithMain -f examples1.txt

error executing java class file

I have the following file ~/workspace/A/A.java:
package A;
public class A{
public static void main(String[]args){
System.out.println("Hello world");
}
}
compiling with ecj don't give me problems,but then I get this error when tring to execute:
~/workspace/A $ java A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
~/workspace/A $ java -cp . A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: A/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I tried this on Linux Mint 17 with openjdk-7-jre version 7u55-2.4.7-1ubuntu1
As you said you have the structure /workspace/A/A.java
just open the terminal and browse to workspace directory then type the following command
For compiling
javac A/A.java
For executing
java A.A
did you try compiling your class first and then executing it
javac A.java
java A
1st you have to compile java program, using below command.
javac A.java
Then, run java A
First you need to compile using javac A.java
then you also need to specify classpath while running like
java -classpath . A
Don't know if ecj differs from javac, so make sure the A.class file has ended up in same dir as your java file (~/workspace/A/) after compiling and try this:
cd ~/workspace
java -cp . A.A
I recommend using a JAVA package name in lowercase especially when your classname is same as the package name. You might want to read this:
http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

Jruby error (wrong name: ruby/jit/FILE_D40BB14F6D953C9F0017A388A07364EC0E5B3F6E)

I am trying to compile a file using jruby with the following commands but I have not had any luck.
jruby -S jrubyc test5.rb
java -cp .:/Users/Ted/.rvm/rubies/jruby-1.6.2/lib test5
I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: test5 (wrong name: ruby/jit/FILE_D40BB14F6D953C9F0017A388A07364EC0E5B3F6E)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:698)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Ted
You need to package your class in a jar file.
Also, your -cp argument needs to specify an actual jar file (or zip) - it cannot refer to a directory as in your example.

Why is my jar producing a NoClassDefFoundError when the class is listed in MANIFEST.MF?

I have created jar in the following folder usr/local/bin/niidle.jar and my MANIFEST.MF file is as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: hector-0.6.0-17.jar
I verified that the file hector-0.6.0-17.jar is also present in the folder:
/Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
I don't want to give full class-path name in MANIFEST.MF file, because I have to run this jar on another machine. So I gave only jar file name Class-Path=hector-0.6.0-17.jar in MANIFEST.MF file.
In spite of mentioning the Class-Path in MANIFEST.MF file, when I run this using command:
java -jar /usr/local/bin/niidle.jar arguments...
It is showing the error message:
--Exception in thread "main" java.lang.NoClassDefFoundError:
me/prettyprint/hector/api/Serializer
at
com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException:
me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
Please give me a solution for this error message.
Class-Path is relative or absolute to the directory where your jar file is located.
so for your case you have to hector-0.6.0-17.jar to /usr/local/bin

Categories

Resources