I have written a java program where I am using a .jar library (Jinterface). I compile it using:
javac -classpath lib/OtpErlang.jar Game.java Actions.java
where the lib dir is inside my project folder.
There is no error when compiling but I get a runtime error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ericsson/otp/erlang/OtpErlangObject
at Game.main(Game.java:7)
Caused by: java.lang.ClassNotFoundException: com.ericsson.otp.erlang.OtpErlangObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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)
... 1 more
And I think that it has to do with the classpath, what is wrong?
Here is the Game class:
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
Actions server = new Actions();
server.moveRight();
}
}
Try running it like this, from the root of all your java packages:
java -classpath lib/OtpErlang.jar Game
This command did the trick:
java -classpath lib/OtpErlang.jar:. Game
I have missed the :. (which tells where my main class is, I suppose...)
Try like this.I hope this will help.....
javac -cp . lib/OtpErlang.jar Game.java Actions.java
Related
I'm on Mac OS X 10.9.2 (Mavericks). I have code that uses an external Java library (twitter4j). It runs fine when I run it through NetBeans. However, trying to run almost identical code in the terminal gives me errors.
My directory structure is straightforward- I have a 'src' folder with the .java file and a 'lib' folder with the external .jar files I use.
From the src folder, I call javac -cp "../lib/*" MyProgram.java which seems to work alright. Now, if I call java MyProgram, I get an exception:
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/StreamListener
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: twitter4j.StreamListener
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)
It seems to have trouble properly importing the external twitter4j library. What am I missing here?
These are my import statements in the code:
import twitter4j.*;, import twitter4j.conf.ConfigurationBuilder;
UPDATE: Using suggestions below, I also tried running it via java -cp "../lib/*" MyProgram which gives: "Could not find or load main class MyProgram"
You need to specify the classpath when running the program as well as when compiling it:
E.g.
java -cp "../lib/*" MyProgram
Reproducing the instructions at
http://stuf.ro/calling-c-code-from-java-using-jna
for calling C code from java I have:
/* ctest.c */
#include <stdio.h>
void helloFromC()
{
printf("Hello from C!\n");
}
This is compiled into the library using:
gcc -o libctest.so -shared ctest.c
Then Java calling program is:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld
{
public interface CTest extends Library
{
public void helloFromC();
}
public static void main(String argv[])
{
CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
ctest.helloFromC();
}
}
Then the java is compiled in Java8 using the -cp classpath argument:
javac HelloWorld.java -cp jna.jar
This step works, producing two class files, HelloWorld.class and HelloWorld$1.class.
The execute step does not work:
java HelloWorld -cp jna.jar
instead producing the following output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Library
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
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)
at HelloWorld.main(HelloWorld.java:14)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
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)
... 13 more
The output is effectively identical to: JNA example program java.lang.NoClassDefFoundError which I have carefully reviewed.
I have run many tests of java8 under cygwin and they all work, this one does not.
I have constructed many permutations of this example, including creating the sun.com.jna directories and including HelloWorld in the package. I have also used different delimiters which are necessary in some circumstances. All of them fail. What am I missing?
It seems that you didn't set your classpath. NoClassDefFoundErrorindicates that your ClassLoadercan't find your compiled classes.
I am currently having a problem I believe deals with some classpath problems but I haven't found any way to fix it. I currently have the following java code:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String url = "http://en.wikipedia.org/wiki/Main_Page";
Document doc = Jsoup.connect(url).get();
String result = doc.text();
System.out.println(result);
}
}
I have the jsoup-1.7.3.jar file in the same directory as the java file(to keep things simple).
I ran the command "javac -cp .:jsoup-1.7.3.jar JsoupTest.java" which compiles and works fine. However when I go and run "java JsoupTest" I get the following error
java JsoupTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at JsoupTest.main(JsoupTest.java:31)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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)
... 1 more
What is the possible error and how exactly would I go about fixing it? I believe it's a classpath issue but the classpath should already be set up when I compiled it? Any help would be appreciated. Thank you.
Just like the compilation command you need to include the jar in the classpath
java -cp .:jsoup-1.7.3.jar JsoupTest
For windows use .; instead of .:
java -cp .;jsoup-1.7.3.jar JsoupTest
Try:
java -cp .:jsoup-1.7.3.jar JsoupTest
I am using IntelliJ, and get a ClassNotFoundException, even when doing a simple Hello World program. After googling and looking at how to add directory to classpath in an application run profile in intellij idea?, I tried changing my dependencies to the JDK folder (/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk), and to no avail.
I have also tried re-installing the JDK and re-installing IntelliJ, both to no avail, and I can run programs in NetBeans without issue. What's going on? Is this some strange bug with IntelliJ?
Code:
public class AppMain {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Error:
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/JObjC.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk:/Applications/IntelliJ IDEA 13 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain AppMain
Exception in thread "main" java.lang.ClassNotFoundException: AppMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
I've found out what sorted it. It was a bug with my import settings, deleting my preferences (i.e. going to ~/Library/Preferences and doing rm -r IdeaIC13) sorted this issue out. Hopefully that's helpful to anyone else coming across this issue.
I'm a new JCuda user and I started to try some samples in my node.
I'm running a simple:
import jcuda.*;
import jcuda.runtime.*;
public class JCudaRuntimeTest{
public static void main(String args[]){
Pointer pointer = new Pointer();
JCuda.cudaMalloc(pointer, 4);
System.out.println("Pointer: "+pointer);
JCuda.cudaFree(pointer);
}
}
I put every library in the same folder and a can easily compile the code, but when I run java JCudaRuntimeTest, I got this exception.
Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/Pointer
at JCudaRuntimeTest.main(JCudaRuntimeTest.java:7)
Caused by: java.lang.ClassNotFoundException: jcuda.Pointer
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
suggestions?
NoClassDefFoundError almost always means that something is missing from your classpath.
Make sure the jcuda-<version>.jar file (as well as possibly other necessary JAR files) are on the classpath when you run your program.
You can specify the classpath when you run your program with the -cp switch, for example:
java -cp C:\Project\jcuda\jcuda-0.3.2a.jar;C:\Project\mystuff\classes org.mystuff.MyProgram
or by setting the CLASSPATH environment variable (not recommended).