Java: Error: Can not find or load main class

advertisements

I am trying to call Java code from an executable. My Java code is as follows:

CostCalculatorType.java:

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

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. How can I get the desired output without using the -cp option?


You need to add to the classpath the directory where your own sources reside.

If that is the current directory, then

export CLASSPATH=.:$CLASSPATH