Linux 错误:无法找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7485670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Could not find or load main class
提问by Dave
I am having trouble compiling and running my Java code, intended to allow me to interface Java with a shared object for Vensim, a simulation modeling package.
我在编译和运行我的 Java 代码时遇到问题,目的是让我将 Java 与一个仿真建模包 Vensim 的共享对象连接起来。
The following code compiles without error:
下面的代码编译没有错误:
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
However, when I try to run the following:
但是,当我尝试运行以下命令时:
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
I get the following error: "Error: Could not find or load main class SpatialModel ". My SpatialModel.java code does contain a 'main' method (below), so I'm not sure what the problem is - can anyone please help me out? Thanks.
我收到以下错误:“错误:无法找到或加载主类 SpatialModel”。我的 SpatialModel.java 代码确实包含一个“main”方法(如下),所以我不确定问题是什么 - 任何人都可以帮我吗?谢谢。
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
public class SpatialModel {
private VensimHelper vh;
public static final String DLL_LIBNAME_PARAM = "vensim_lib_nam";
public static final String MODEL_PATH_PARAM = "vensim_model_path";
private final static int VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT = 10;
public SpatialModel() throws SpatialException {
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if(libName == null || libName.trim().equals("")) {
log.error("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
throw new SpatialException("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
}
if(modelPath == null || modelPath.trim().equals("")) {
log.error("Model path has to set with -D" + MODEL_PATH_PARAM);
throw new SpatialException("Model path ahs to be set with -D" + MODEL_PATH_PARAM);
}
for (int i = 0; i < VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT && vh == null; i++) {
try {
log.info("creating new vensim helper\n\tdll lib: " + libName + "\n\tmodel path: " + modelPath);
vh = new VensimHelper(libName, modelPath);
} catch (Throwable e) {
log.error("An exception was thrown when initializing Vensim, try: " + i, e);
}
}
if (vh == null) {
throw new SpatialException("Can't initialize Vensim");
}
}
public static void main(String[] args) throws VensimException {
long before = System.currentTimeMillis();
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if (libName == null) {
libName = "libvensim";
}
if(modelPath == null) {
modelPath = "~/BassModel.vmf";
}
System.setProperty(DLL_LIBNAME_PARAM, libName);
System.setProperty(MODEL_PATH_PARAM, modelPath);
if (args.length > 0 && args[0].equals("info")) {
System.out.println(new VensimHelper(libName, modelPath).getVensimInfo());
} else if (args.length > 0 && args[0].equals("vars")) {
VensimHelper helper = new VensimHelper(libName, modelPath);
String[] vars = helper.getVariables();
for (String var : vars) {
System.out.println(helper.getVariableInfo(var));
}
} else {
File f = new File(".");
System.out.println(f.getAbsolutePath());
SpatialModel sm = new SpatialModel();
}
System.out.println("Execution time: " + (System.currentTimeMillis() - before));
}
}
采纳答案by Saket
You must ensure that you add the location of your .class
file to your classpath. So, if its in the current folder, add .
to your classpath.
Note that the Windows classpath separator is a semi-colon, i.e. a ;
.
您必须确保将.class
文件的位置添加到类路径中。因此,如果它在当前文件夹中,请添加.
到您的类路径中。请注意,Windows 类路径分隔符是一个分号,即;
.
回答by Rafael Cordones
I believe you need to add the current directory to the Java classpath
我相信您需要将当前目录添加到 Java 类路径中
java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
回答by ymutlu
Problem is not about your main function. Check out for
问题不在于您的主要功能。退房
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
output and run it.
输出并运行它。
回答by Shaddu
If you try to run a java application which needs JDK 1.6 and you are trying to run on JDK 1.4, you will come across this error. In general, trying to run a Java application on old JRE may fail. Try installing new JRE/JDK.
如果您尝试运行需要 JDK 1.6 的 Java 应用程序,并且尝试在 JDK 1.4 上运行,则会遇到此错误。通常,尝试在旧 JRE 上运行 Java 应用程序可能会失败。尝试安装新的 JRE/JDK。
回答by Sudhakar Pabbati
You can try these two when you are getting the error: 'could not find or load main class'
出现错误时,您可以尝试这两个:“无法找到或加载主类”
If your class file is saved in following directory with HelloWorld
program name
d:\sample
如果您的类文件以HelloWorld
程序名称
保存在以下目录中d:\sample
java -cp d:\sample HelloWorld
java -cp . HelloWorld
java -cp d:\sample HelloWorld
java -cp . HelloWorld
回答by Samsky
Check your BuildPath
, it could be that you are referencing a library that does not exist anymore.
检查您的BuildPath
,可能是您正在引用一个不再存在的库。
回答by Snekse
I know this question was tagged with linux, but on windows, you might need to separate your cp args with a ;
instead of a :
.
我知道这个问题是用 linux 标记的,但是在windows 上,您可能需要用 a;
而不是 a来分隔您的 cp 参数:
。
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
回答by user2318595
You have to set the classpath if you get the error:
如果出现错误,则必须设置类路径:
Could not find or load main class XYZ
无法找到或加载主类 XYZ
For example:
例如:
E:\>set path="c:\programfiles\Java\jdk1.7.0_17\bin"
E:\>set classpath=%classpath%;.;
E:\>javac XYZ.java
E:\>java XYZ
回答by cane
If you work in Eclipse, just make a cleanup (project\clean..
clean all projects
) of the project.
如果您在 Eclipse 中工作,只需project\clean..
clean all projects
对项目进行清理 ( )。
回答by Maxim Geerinck
I was using Java 1.8, and this error suddenly occurred when I pressed "Build and clean" in NetBeans. I switched for a brief moment to 1.7 again, clicked OK, re-opened properties and switched back to 1.8, and everything worked perfectly.
我使用的是 Java 1.8,当我在 NetBeans 中按下“Build and clean”时突然出现这个错误。我又短暂地切换到 1.7,单击“确定”,重新打开属性并切换回 1.8,一切正常。
I hope I can help someone out with this, as these errors can be quite time-consuming.
我希望我能帮助别人解决这个问题,因为这些错误可能非常耗时。