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

}

当前回答

如果您正在使用Eclipse……我重命名了我的主类文件,得到了这个错误。我转到“Run As”配置器,在该项目的类路径下,它在类路径中列出了两个文件。我删除了我重命名的旧类,并留下了具有新名称的类,它编译和运行正常。

其他回答

如果您试图运行一个需要JDK 1.6的java应用程序,而您试图在JDK 1.4上运行,您将遇到这个错误。通常,尝试在旧的JRE上运行Java应用程序可能会失败。尝试安装新的JRE/JDK。

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

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

如果类在包中

package thepackagename;

public class TheClassName {
  public static final void main(String[] cmd_lineParams)  {
     System.out.println("Hello World!");
  } 
}

然后调用:

java -classpath . TheClassName

导致错误:无法找到或加载主类classname。这是因为它必须用它的全限定名调用:

java -classpath . thepackagename.TheClassName

并且这个thepackagename目录必须存在于类路径中。在本例中,.表示当前目录,是整个类路径。因此,必须从packagename所在的目录调用这个特定的示例。

需要明确的是,这个类的名称不是classname,而是packagename. classname。尝试执行classname不能工作,因为不存在具有该名称的类。反正不在当前类路径上。

最后,请注意执行的是编译后的(.class)版本,而不是源代码(.java)版本。因此“路径”。

如果您正在使用Eclipse……我重命名了我的主类文件,得到了这个错误。我转到“Run As”配置器,在该项目的类路径下,它在类路径中列出了两个文件。我删除了我重命名的旧类,并留下了具有新名称的类,它编译和运行正常。

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.