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

}

当前回答

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

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

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

其他回答

如果类在包中

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

我知道这个问题被标记为linux,但在windows上,你可能需要用一个;而不是a:。

java -cp . - apache4 - j- 1.16 /log4j-1.2.16 jar; /vensim.jar SpatialModel vars

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

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

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

输出并运行它。

我使用Anypoint Studio(一种基于Eclipse的IDE)。在我的例子中,一切都很好,直到我发现在运行java代码时,执行了完全不同的东西。然后我删除了.class文件。在此之后,我从这个问题的标题中得到了错误消息。清理项目并没有解决问题。

重新启动IDE后,一切都恢复正常。

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.