问题:
javac 编译完源码 ,执行 java Mainclassname 报 “Error: Could not find or load main class ***”错误
原因:
运行程序无法找到主类,可能是没有写完整的主类名字(需要完整包名)或者主类名字写错(不是写的主类的名字而是写的文件名),也可能是没有指定类路径的目录。
解决方法1:
java -classpath [类路径的目录] package1.
例如:
- 在 ***/target/classes/hello 目录运行命令为
java -cp ../ hello/HelloWorld
或者
java -cp ../
(这里的 -classpath 也可以用 -cp)
注意1:-classpath 后面的路径为*包名(package1)所在目录及在package1文件夹所在目录(如果源程序开始没有 package 任何包 ,那就是编译后的当前目录,-cp ./ 当前路径可以不用指定-cp参数) ,或者 ../target/classes/ 的相对或绝对路径
注意2:运行的文件一定是完整名称,要添加所有的包名(如果程序开始有 package packagename)一直到主类的名字如:packagename.Mainclassname(注意这里是主类的名字不是主类所在文件的名字,也不是生成的***.class文件,是main方法所在的类的名字)
解决方法2
如果有包名的话,cd 到类路径的目录(*包名package1文件夹所在目录 ,或者 ../target/classes/目录下),如果源程序开始没有 package 任何包,那就在编译后产生 ***.class 文件所在路径运行如下命令。
java package1.
例如:
- 在***/target/classes 目录运行如下命令 package名 hello 所在目录
java hello/HelloWorld
或者
java
总结:
此问题是找不到主类引起的,运行的时候一定是完整的主类名字包括包名,而且需要指定类的路径的目录。
附录:
运行 java -help 后java 使用方法和参数表
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
Warning: this feature is deprecated and will be removed
in a future release.
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: this feature is deprecated and will be removed
in a future release.
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, . -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see
-splash:<imagepath>
show splash screen with specified image
See /technetwork/java/javase/documentation/ for more details.
- :(注意 package 名)
package hello;
public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
(());
}
}
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
参考:
/questions/18093928/what-does-could-not-find-or-load-main-class-mean