0

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?

4
  • Does your Client.java have package snmpclient; instruction in the top? Give us the error text also, it helps a lot. Commented Jun 19, 2012 at 13:30
  • Yep I do have the package outlined on the Client.java class. I'll paste the errors right now. Commented Jun 19, 2012 at 13:32
  • @BrianAgnew below got it right. Check out his answer. Commented Jun 19, 2012 at 13:34
  • Yes, I was thinking along the same lines just now: where your classes get compiled to, you need to tell Java about this location. Commented Jun 19, 2012 at 13:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That second line worked like a charm. I didn't understand yet what the ":." do after the classpath though. Could you explain? Thanks a lot.
I've amended the answer appropriately. Let me know if no good
Let me see if I got this: Without the colon the command won't work cause it will interpret everything as the classpath. With the colon it manages to separate what's the classpath and what's the class you want to invoke. Is that it? Anyway thanks, it helped a lot.
You need to specify the path to your classes (that's the dot) and the jar file that contains the 3rd party classes (that's the jar file). You can specify multiple entries in the classpath (e.g. many .jar files plus a couple of directories etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.