错误:找不到或加载主类

问题:

我无法编译和运行我的Java代码,旨在让我将Java与一个模拟建模软件Vensim的共享对象进行接口。
以下代码无误编译:

javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java     VensimHelper.java VensimException.java VensimContextRepository.java

但是,当我尝试运行以下操作时:

java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars

我收到以下错误:“错误:找不到或加载主类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 &amp;&amp; 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&#91;&#93; 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 &amp;&amp; args[0].equals("info")) {
            System.out.println(new VensimHelper(libName, modelPath).getVensimInfo());
        } else if (args.length > 0 &amp;&amp; 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));
    }

}

回答:

您必须确保将.class文件的位置添加到您的类路径中。所以,如果它在当前文件夹中,然后添加.到你的类路径。
请注意,Windows类路径分隔符是分号,即;

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: Error: Could not find or load main class

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

62 + = 66