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

}

当前回答

如果类在包中

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)版本。因此“路径”。

其他回答

你必须设置类路径,如果你得到错误:

无法找到或加载主类XYZ

例如:

E:\>set path="c:\programfiles\Java\jdk1.7.0_17\bin"
E:\>set classpath=%classpath%;.;
E:\>javac XYZ.java
E:\>java XYZ

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.

如果类在包中

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中工作,只需要做一个清理(project\clean..清理项目的所有项目)。

问题不在于你的主要职能。检查

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

输出并运行它。