简体   繁体   English

我在命令中指定目录时出现Java类路径问题

[英]Java classpath issue when I specify the directory in the command

When I try running my Java application, it cannot find the main class in the directory I specify. 当我尝试运行Java应用程序时,它在我指定的目录中找不到主类。

My application is in this directory: 我的应用程序在以下目录中:

C:\\Users\\Cristian\\git\\tranquil-crag-4851

My main class is in this directory: 我的主要班级在这个目录中:

C:\\Users\\Cristian\\git\\tranquil-crag-4851\\target\\classes\\com\\example

My main class is called 我的主班叫

Main.class

When I run this command 当我运行此命令时

java -cp target/classes:target/dependency/* com.example.Main

from directory 从目录

C:\\Users\\Cristian\\git\\tranquil-crag-4851\\

the result I get is: 我得到的结果是:

Could not find the main class in com.example.Main 在com.example.Main中找不到主类

My CLASSPATH variable is set to JAVA_HOME\\lib . 我的CLASSPATH变量设置为JAVA_HOME\\lib Can somebody explain to me how to solve this? 有人可以向我解释如何解决吗?

The delimiter : works on Unix. 定界符:在Unix上有效。 You should use ; 你应该用; on Windows instead. 在Windows上代替。 I am not sure that forward slashes work on Windows in this context. 我不确定在这种情况下Windows上是否可以使用正斜杠。 Try to use back slashes instead, ie 尝试改用反斜杠,即

java -cp target\\classes;target\\dependency\\* com.example.Main

If you are using java 1.6 or higher this should work. 如果您使用的是Java 1.6或更高版本,则应该可以使用。 I mean that * was not supported by previous versions of java. 我的意思是Java的早期版本不支持*

If something does not work, check the path. 如果不起作用,请检查路径。 Go to the directory where you are running your application, type 转到运行应用程序的目录,键入

dir target\classes
dir target\dependency\

and see that the output of these commands is as expected. 并看到这些命令的输出符合预期。

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

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