错误:找不到或加载主类[重复]

在尝试运行Java代码时,遇到'Error: Could not find or load main class'的问题。尽管代码已成功编译,但在执行时出错。讨论涉及检查类路径、在Windows上正确分隔cp参数、设置类路径以及可能与IDE(如Eclipse和NetBeans)相关的问题。解决方案包括清理项目、检查使用的Java版本和确保类路径指向正确的库。
摘要由CSDN通过智能技术生成

本文翻译自:Error: Could not find or load main class [duplicate]

This question already has answers here : 这个问题已经在这里有了答案
Closed 3 years ago . 3年前关闭。

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 ". 我收到以下错误:“错误:找不到或加载主类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? 我的SpatialModel.java代码确实包含一个“主要”方法(如下),所以我不确定是什么问题-有人可以帮我吗? Thanks. 谢谢。

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));
    }

}

#1楼

参考:https://stackoom.com/question/VPMc/错误-找不到或加载主类-重复


#2楼

检查您的BuildPath ,可能是您正在引用一个不存在的库。


#3楼

I know this question was tagged with linux, but on windows , you might need to separate your cp args with a ; 我知道这个问题是用Linux标记的,但是在Windows上 ,您可能需要使用;分隔cp args ; instead of a : . 代替:

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


#4楼

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

#5楼

如果您使用Eclipse,只需对project\\clean..进行清理( project\\clean.. clean all projects )。


#6楼

I was using Java 1.8, and this error suddenly occurred when I pressed "Build and clean" in NetBeans. 我使用的是Java 1.8,当我在NetBeans中按下“ Build and clean”时突然发生此错误。 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. 我再次短暂切换到1.7,单击“确定”,重新打开属性,然后切换回1.8,一切正常。

I hope I can help someone out with this, as these errors can be quite time-consuming. 我希望我可以帮助别人解决这些问题,因为这些错误可能会非常耗时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值