简体   繁体   中英

Java compilation/runtime classpath problems

I am trying to build a Java SNMP client. I have a folder on my Ubuntu desktop called snmpclient. Inside that folder I have the main class Client.java and the snmp.jar library, which is used by the main class.

I managed to compile it sucessfully using the following command on the terminal:

~/Desktop$ javac snmpclient/Client.java -classpath ./snmpclient/snmp.jar 

Then I tried to run it with this command:

~/Desktop$ java snmpclient.Client -classpath ./snmpclient/snmp.jar

But I am getting a "java.lang.ClassNotFoundException" error, saying it can't find the classes of the snmp library. I unzipped the jar file to make sure the classes I am using are all there, and they are.

Any idea on how I can solve this?

I would rearrange your args thus:

~/Desktop$ java -classpath ./snmpclient/snmp.jar snmpclient.Client

such that your classpath preceeds the class to run. Note that your classpath defaults to the current directory if you don't specify -classpath , so your full invocation should be:

~/Desktop$ java -classpath ./snmpclient/snmp.jar:. snmpclient.Client

to specify the root directory where your classes reside (that's the dot), plus the SNMP jar file.

The -classpath arg consists of jar files and paths to directories separated by colons. See here for more info on setting the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM