Daniel Scocco
Daniel Scocco

Reputation: 7266

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?

Upvotes: 0

Views: 1119

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272407

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.

Upvotes: 1

Related Questions