简体   繁体   English

尝试在命令行上运行时,Java“无法找到或加载主类”

[英]Java “cannot find or load main class” when trying to run on command line

I have a java project with the directory structure as follows: 我有一个Java项目,其目录结构如下:

java 
   | build
   | src
     | gui
         |  Launcher.java

I am compiling the program to a .class file using the command: 我正在使用以下命令将程序编译为.class文件:

find -name "*.java" > src.txt
javac -d build @src.txt
rm $src.txt

After compilation the project directory looks like this 编译后,项目目录如下所示

java 
   | build
     | gui
        |   Launcher.class 
   | src
     | gui
         |  Launcher.java

When I try to run the program (main is in Launcher.java) with: 当我尝试使用以下命令运行程序时(主要在Launcher.java中):

java -cp .:build:**/*.class gui.Launcher

I get an error saying: 我收到一条错误消息:

Error: Could not find or load main class gui.Launcher

Anyone see whats wrong? 有人看到错了吗?

note: I am using cygwin on windows for this 注意:我正在Windows上使用cygwin

Because your classpath syntax is off. 因为您的类路径语法已关闭。

java -cp .:build:**/*.class gui.Launcher

should be (on Unix) 应该是(在Unix上)

java -cp .:build/ gui.Launcher

or (on Windows) 或(在Windows上)

java -cp .;build/ gui.Launcher

The other answer is correct for Linux/Unix and for Windows (without Cygwin). 另一个答案对于Linux / Unix和Windows(无Cygwin)是正确的。 However, when working with Java from Cygwin there's a mismatch in that Cygwin is trying to act like Linux/Unix, but Java is a Windows binary and so it doesn't understand the Linux/Unix path syntax. 但是,当从Cygwin使用Java时,存在一个不匹配之处,即Cygwin试图像Linux / Unix一样工作,但是Java是Windows二进制文件,因此它不了解Linux / Unix路径语法。

Here's what you need to do when running java (or javac ) with a class path from Cygwin. 这是使用来自Cygwin的类路径运行java (或javac )时需要做的事情。 First use a semicolon as the path separator, and second put the whole path in quotes: 首先使用分号作为路径分隔符,然后将整个路径用引号引起来:

java -cp 'some/directory;path/to/file.jar;etc' main.class.Name <arguments>

Note that you can also try using the cygpath utility to convert a Linux/Unix style path to a Windows format that Java can understand. 请注意,您还可以尝试使用cygpath实用程序将Linux / Unix样式路径转换为Java可以理解的Windows格式。

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

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