Problem
I’m having difficulties building and running Java code that’s supposed to connect Java to a common object in Vensim, a simulation modeling software.
The code below compiles without errors:
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
When I try to run the following command:
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
“Error: Could not discover or load main class SpatialModel,” says the error message. I have a’main’ function in my SpatialModel.java code (below), so I’m not sure what the issue is – can somebody possibly help me out? Thanks.
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));
}
}
Asked by Dave
Solution #1
You must add your.class file’s location to your classpath. Add. to your classpath if it’s in the current folder. Note that the Windows classpath separator is a semi-colon, i.e. a ;.
Answered by Saket
Solution #2
If the class is in a package
package thepackagename;
public class TheClassName {
public static final void main(String[] cmd_lineParams) {
System.out.println("Hello World!");
}
}
Then calling:
java -classpath . TheClassName
as a result of Error: The primary class TheClassName could not be found or loaded. This is due to the fact that it must be referred to by its entire name:
java -classpath . thepackagename.TheClassName
In addition, thepackagename must be present in the classpath. The complete classpath in this example is., which stands for the current directory. As a result, this example must be run from the directory where thepackagename is located.
To be clear, this class’s name is thepackagename, not TheClassName. TheClassName. Because no class with that name exists, attempting to execute TheClassName fails. In any case, it’s not on the current classpath.
Finally, the compiled (.class) version, not the source code (.java) version, is run. As a result, “CLASSPATH” was coined.
Answered by aliteralmind
Solution #3
When you get the problem, try these two options: ‘Main class could not be found or loaded’
If your HelloWorld application is named d:sample and your class file is saved in the following location,
Answered by Sudhakar Pabbati
Solution #4
I believe the current directory should be added to the Java classpath.
java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
Answered by Rafael Cordones
Solution #5
You have to include classpath to your javac and java commands
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
suppose you have the following
com.test is the name of the package. Class Name: Hello (Having main) file is located inside “src/com/test/Hello.java”
from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Answered by Muhammad Soliman
Post is based on https://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class