我在编译和运行我的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));
    }

}

当前回答

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

javac HelloWorld.java && java HelloWorld.class

当我应该删除。class:

javac HelloWorld.java && java HelloWorld

其他回答

This problem occurred for me when I imported an existing project into eclipse. What happens is it copied all the files not in the package, but outside the package. Hence, when I tried run > run configurations, it couldn't find the main method because it was not in the package. All I did was copy the files into the package and Eclipse was then able to detect the main method. So ultimately make sure that Eclipse can find your main method, by making sure that your java files are in the right package.

我认为您需要将当前目录添加到Java类路径中

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

如果遇到这个错误,并且正在使用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中。

如果比很多人想的简单,包括我在内:)

cd到项目文件夹/src/package,在那里你会看到yourClass.java,然后运行javac yourClass.java,它会创建yourClass.class,然后从src文件夹中cd到build文件夹中,你可以运行java package. youclass

我在Mac上使用终端,或者你可以在windows上使用命令提示符完成相同的任务

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