Search code examples
javalinuxjavac

Compiling and running a java program with complex file structure from the commandline


Trying to compile and run my java program from the commandline that is set up a bit weird. The file structure is as follows:

[ROOT]/
|
|____libs/
|    |____myExtraJar.jar
|     
|____src/
     |____main/
          |____com/
               |____example/
                    |____myClass.java

The package is defined at the top of the java file as

package com.example;

I am able to compile the program fine (I think) while in the root folder, using

javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java

I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. However, I can't find any way to run the compiled program. Running

java src/main/com/example/myClass

results in the message

Error: Could not find or load main class src.main.com.example.myClass

Any help would be appreciated.


Solution

  • You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. Like,

    java -cp "libs/myExtraJar.jar:src/main" com.example.myClass