java - 错误 : Could not find or load main class

标签 java linux

<分区>

我在编译和运行我的 Java 代码时遇到问题,旨在让我将 Java 与 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 代码确实包含一个“主要”方法(如下),所以我不确定问题出在哪里 - 谁能帮帮我?谢谢。

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));
    }

}

最佳答案

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

关于java - 错误 : Could not find or load main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16579109/

相关文章:

java - 为java编写自定义 Sonar 规则时如何处理assertionError

linux - 是否有命令行(终端)程序来监视Linux中的磁盘使用情况?

linux - 如何在 red hat Linux shell 脚本中获取指定用户的桌面文件夹

linux - 如何列出目录及其所有子目录中的所有文件?

php - 如何在 Linux 系统上安装 PHP 的 id3_get_tag

Java Ajax 请求参数具有空值

java - ANTLR - 如何确定什么样的解析树 "best fits"一些代码

java - 在 NetBeans 中运行 Maven 目标的按钮

java - 具有多个实体的 JPA 连接表

linux - uclibc 和 arm 支持上的 Node.js