简体   繁体   English

Java编译/运行时类路径问题

[英]Java compilation/runtime classpath problems

I am trying to build a Java SNMP client. 我正在尝试构建Java SNMP客户端。 I have a folder on my Ubuntu desktop called snmpclient. 我在Ubuntu桌面上有一个名为snmpclient的文件夹。 Inside that folder I have the main class Client.java and the snmp.jar library, which is used by the main class. 在该文件夹中,我有主类Client.java和主类使用的snmp.jar库。

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. 但是我收到“ java.lang.ClassNotFoundException”错误,说它找不到snmp库的类。 I unzipped the jar file to make sure the classes I am using are all there, and they are. 我解压缩了jar文件,以确保我正在使用的类全部存在,并且已经存在。

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: 请注意,如果您未指定-classpath ,则您的类路径默认为当前目录,因此您的完整调用应为:

~/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. 指定类所在的根目录(即点) 以及 SNMP jar文件。

The -classpath arg consists of jar files and paths to directories separated by colons. -classpath arg由jar文件和以冒号分隔的目录路径组成。 See here for more info on setting the classpath. 有关设置类路径的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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