简体   繁体   English

如何在Windows命令行中使用Java类路径?

[英]How to use java classpath in windows command line?

I have created a java source file in Source folder and also created a folder named classes which is supposed to contain my class files . 我已经在Source文件夹中创建了一个Java源文件,并且还创建了一个名为classes的文件夹,该文件夹应该包含我的类文件。

javac -d classes Source/TestMyAnnotation.java

puts my files in the directory structure classes/Source/... 将我的文件放入目录结构类/ Source / ...

Now I want to run that compiled classes but 现在我想运行编译后的类,但是

java -cp classes/Sources TestMyAnnotation

throwing NoClassDefFoundError Where am I doing wrong? 抛出NoClassDefFoundError我在哪里做错了? How to do it in a correct way? 如何以正确的方式做到这一点?

Sources is not a package folder; Sources不是包文件夹; it's just the directory that holds your java source, so you don't need to put it on your classpath. 它只是保存Java源代码的目录,因此您无需将其放在类路径中。 If you take a look inside your classes directory after you have compiled your java source, you will see a file TestMyAnnotation.class - there's no Sources directory underneath it, so you don't specify it on the classpath. 如果在编译Java源代码后在classes目录中查看,则会看到文件TestMyAnnotation.class它下面没有Sources目录,因此您不必在classpath上指定它。

You will be able to run your class like this: 您将可以像这样运行您的课程:

java -cp classes TestMyAnnotation

You can find more information on how all this works on Oracle's Managing Source and Class Files Page 您可以在Oracle的“ 管理源文件和类文件”页面上找到有关所有这些工作原理的更多信息。

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

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