I'm trying to check some SNMP example, so
i've downloaded snmp4j.jar
compiled SNMP.java using javac -cp snmp4j.jar SNMP.java (it's OK)
tried to start it using java -cp snmp4j.jar SNMP, but
it reports:
root#comp:~/workspace_c/SNMP# java -cp snmp4j.jar SNMP
Exception in thread "main" java.lang.NoClassDefFoundError: SNMP
Caused by: java.lang.ClassNotFoundException: SNMP
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)
Could not find the main class: SNMP. Program will exit.
UPDATE:
And when I import snmp4j.jar to the java project, it works normally in ECLIPSE...
UPDATE#2: main part of SNMP.java
import org.snmp4j.CommunityTarget;
...
public class SNMP {
public static void main(String[] args) {
...
}
}
what i'm doing wrong? it's first time i've met snmp4j so i know noting about it
If I understand correctly, SNMP is your example class, which isn't inside the jar. In that case, you need to add to the classpath both the jar and your current directory:
java -cp .:snmp4j.jar SNMP
As far as I can see you are trying to run your application with SNMP4J.jar (upper case) but when you compiled your application you probably used lower case version.
Your prompt shows that your are using unix OS that has case sensitive file system. So, check the case of the JAR file name. I believe that all letters are in lower case.
Related
This program compiles and runs swimmingly in eclipse on the same machine, but I'd really like to work from the command line and my editor of choice.
CollectNewspaperKarel.java
import stanford.karel.*;
public class CollectNewspaperKarel extends SuperKarel {
public void run() {
// You fill in this part
}
}
The karel.jar is in the same directory as the file above.
Compile
javac -cp karel.jar CollectNewspaperKarel.java
with no errors.
Run
java -cp karel.jar CollectNewspaperKarel
Exception in thread "main" java.lang.NoClassDefFoundError: CollectNewspaperKarel
Caused by: java.lang.ClassNotFoundException: CollectNewspaperKarel
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
you forgot to include your class to classpath when running the program.
Try this - if running on windows:
java -cp karel.jar;CollectNewspaperKarel.class CollectNewspaperKarel
or this if you're running on linux:
java -cp karel.jar:CollectNewspaperKarel.class CollectNewspaperKarel
One more thing however, you need to have main method in your class, otherwise it won't work :)
Update:
I've found following site: http://ycsoftware.net/setting-up-karel-the-robot-in-eclipse/
seems, you should go with the following arguments if you have the same version of karel as the author there:
on windows:
java -cp karel.jar;CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
on linux:
java -cp karel.jar:CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
Peter B. is right, however you cannot run a class containing a "run" method alone, you need a public static void(String[] args) method to make it runnable.
I suppose that in Eclipse some other class is used as a "main class" to run the thing.
I'm trying to create a simple Java Agent program before I implement the real thing. I cannot get it to run. Clearly I have some sort of configuration or class path problem. No amount of looking and trying has lead to me the problem.
If I run:
java -cp ./demoAgent.jar -javaagent:./demoAgent.jar com.kingtigerbooks.demoMod.Main
I get the following error:
Exception in thread "main" java.lang.ClassNotFoundException: com.kingtigerbooks.demoAgent.Agent
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:280)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338)
FATAL ERROR in native method: processing of -javaagent failed
Abort trap
I'm running this on my Mac. The jar file with the agent is in the current directory. The manifest for the jar file looks like this:
Manifest-Version: 1.0
Premain-class: com.kingtigerbooks.demoAgent.Agent
Can-Redefine-Classes: true
If smells like a class path problem, but as you can see I have included the jar on the class path. Any help would be appreciated. Its a very simple project.
Problem solved. I had a bad jar file. The agent jar file did not contain the agent class. By building the correct jar file and putting the fully qualified path to the jar on in the javaagent parameter it all works.
when you run an agent, you do not add the agent jar to the normal classpath.
as a side note, you can add -verbose:class to the command line to get info about the class loading process.
The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. I wrote that on windows on eclipse but want to compile it on Linux javac compiler.
javac filter.java ClasifiedWord.java
runs without mistakes, but when I try to run the program:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
How can I run the program ? It seems that the compilation is ok. I have doubts that maybe I have made a mistake about the file paths in the program.... but I think that this is not the case?
Thanks for your answers!
Type the following command.
java -classpath . filter
If you want to type just java filter, please following these guides.
Linux http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/
Windows http://ac-support.europe.umuc.edu/~arnoldyl/NetBeansTutorials/Setting-Classpath.html
P.S. The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument.
Roland is right in the comment, you should be able to run it with java -classpath <folderContainingDotClassFiles> filter. Also, you should change filter.java to Filter.java as the standard Java convention for class names (including the files containing them) is to capitalize the first letter.
This looks like you either have a wrong CLASSPATH setting (usually you don't need this at all, but if you have it, it should include . in the list, for the current directory), or you are using packages (wrongly), or both. For the classpath issue, see the other answers.
If you are using packages, i.e. your files contain a package ...; line at the beginning, you should put your source files in a directory structure according to the package structure. For example, if you have package example;, you should have an example directory in your current directory, and in this the two source files.
Then call compiler and interpreter like this:
javac example/filter.java example/ClasifiedWord.java
java example.filter
(This assumes that filter is the main class, otherwise replace its name.)
I have built a small program for school that runs perfectly when i run it through eclipse, but if I run the same code in NetBeans I get this error:
java.lang.NoClassDefFoundError: myrunnable/Main
Caused by: java.lang.ClassNotFoundException: myrunnable.Main
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)
Could not find the main class: myrunnable.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
How do i fix this?!
I have not used netbeans but from what I can see from your exception looks to me JRE may not be in the classpath. Have you checked that? For example when you create a project in eclipse it will automatically add JRE to the classpath.
OR
may be you need to explicitly compile/build before you can run. In eclipse it is auto too. When you run a simple.java it would have created the corresponding simple.class in the bin output folder.
Check those two areas you will be fine I think
Is there a main method defined in the class?
public static void main(String[] args) {
}
Also, is your Java JDK properly registered inside Netbeans?
The title pretty much says it all, I am trying to get a java program (.jar file) to run from a Perl program. I read another post on Stackoverflow saying this syntax is correct:
system("java filename.jar");
However this is giving me the following error. I'm not sure if the problem is that it shows the filename as "filename/jar" instead of "filename.jar"
Exception in thread "main" java.lang.NoClassDefFoundError: filename/jar
Caused by: java.lang.ClassNotFoundException: filename.jar
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)
You need to call it as java -jar filename.jar.
If you leave out the -jar, then java thinks you're trying to run a class named filename.jar, which it tries to load from the filename/jar file. Since you don't have that file, java throws the error that you see.
You have to use the -jar option to launch a jar.
java [ options ] -jar file.jar [ argument ... ]
Resources :
sun.com - java