Alex Rosenfeld Alex Rosenfeld - 1 year ago 58
Java Question

Compiling and running java program with other programs on build path

I'm having trouble getting my program to run in the command line. projectA has projectB and projectC on the build path.

I run this command:

javac -cp "projectB/src/":"projectC/src/" path/to/projectA/src/packagename/

This compiles fine. All the .java files in projectA, projectB, and projectC compile into .class files. However, when I run the command:

java -cp "projectB/src/":"projectC/src/" path/to/projectA/src/packagename/Program

I get the following error:

Error: Could not find or load main class

I have tried running the java command with many different derivatives of the -cp, paths, and from different working directories. Thanks for the help!

Answer Source

With your commands, javac thinks that the package is path/to/projectA/src/packagename/Program

You need to specify the fully qualified name (packagename.Program) and add the path in your classpath:

java -cp "projectB/src/":"projectC/src/":"path/to/projectA/src" packagename.Program