我在编译和运行我的Java代码时遇到了麻烦,旨在允许我将Java与Vensim的共享对象进行接口,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 && 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));
    }

}

当前回答

清理项目>,然后确保BuildPath > Libraries具有正确的库。

其他回答

您必须在javac和java命令中包含类路径

javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main

假设您有以下内容

包名为:com.test 班级名称:Hello(有主) 文件位于src/com/test/Hello.java

从外部目录:

$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello

在窗户上,同样的事情也会起作用,我已经尝试过了

我得到这个错误是因为我试图运行

javac HelloWorld.java && java HelloWorld.class

当我应该删除。class:

javac HelloWorld.java && java HelloWorld

如果遇到这个错误,并且正在使用Maven构建jar,那么很有可能只是在src/main/ Java /中没有Java类。

在我的例子中,我在Eclipse中创建了我的项目,默认为src(而不是src/main/java/)。

所以我最终得到了类似于mypackage.morepackage.myclass的东西,以及类似于src/mypackage/morepackage/myclass的目录结构,这本身没有任何问题。但是当你运行mvn clean install时,它会查找src/main/java/mypackage/morepackage/myclass。它不会找到类,但也不会出错。所以它会成功构建,当你运行输出的Jar时,结果是:

Error: Could not find or load main class mypackage.morepackage.myclass

因为它从来没有将您的类包含在打包的Jar中。

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

你可以尝试这两个当你得到错误:'无法找到或加载主类'

如果你的类文件保存在HelloWorld程序名下面的目录中 d: \样本

java -cp d:\sample HelloWorld Java -cp。HelloWorld