简体   繁体   English

Java:错误:找不到或加载主类

[英]Java: Error: Could not find or load main class

I am trying to call Java code from an executable. 我试图从可执行文件中调用Java代码。 My Java code is as follows: 我的Java代码如下:

CostCalculatorType.java: CostCalculatorType.java:

public interface CostCalculatorType {
    public double calculateCost(double[] chromosome);
}

Main.java: Main.java:

import org.plyjy.factory.JythonObjectFactory;

public class Main {

    public static void main(String[] args) {
        double[] a = new double[]{1.3653333, 0.0000000, -1.8204444, -1.8204444}; 
double result;

        JythonObjectFactory factory = JythonObjectFactory.getInstance();
        CostCalculatorType costCalc = (CostCalculatorType)
            factory.createObject(CostCalculatorType.class, "CostCalculator");
        result = costCalc.calculateCost(a);
        System.out.println("Result = "+result);
    }
}

When I run the following commands, I get the desired output: 运行以下命令时,将获得所需的输出:

javac -cp ".:/home/ch/PlyJy.jar:/home/ch/jython.jar" CostCalculatorType.java Main.java 
java -cp ".:/home/ch/PlyJy.jar:/home/ch/jython.jar" CostCalculatorType.java Main

Result = 3324.260315871956

However, when I set the classpath and run the following commands, I get an error. 但是,当我设置类路径并运行以下命令时,出现错误。

 export CLASSPATH=/home/ch/jython.jar:$CLASSPATH
 export CLASSPATH=/home/ch/PlyJy.jar:$CLASSPATH
 javac CostCalculatorType.java Main.java 
 java Main

 Error: Could not find or load main class Main

I want to be able to do this without using the -cp option because, I want to strip off the main method, move it to a different method and, call it from a different program. 我希望不使用-cp选项就可以执行此操作,因为我想剥离main方法,将其移至其他方法,然后从其他程序调用它。 How can I get the desired output without using the -cp option? 如何不使用-cp选项而获得所需的输出?

You need to add to the classpath the directory where your own sources reside. 您需要将您自己的源代码所在的目录添加到类路径中。

If that is the current directory, then 如果这是当前目录,则

export CLASSPATH=.:$CLASSPATH

The error suggested that Main cannot be found, where is Main? 该错误提示找不到Main,Main在哪里? It is in your current directory? 它在您当前的目录中吗? In the java -cp , you have set "." 在java -cp中,您已经设置了“。” but not in the second case where you export you classpath. 但在第二种情况下(导出类路径)则不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM