CodeMed
CodeMed

Reputation: 9191

how can i compile a java program to run from a given folder?

I have a java program Test.java located at /path/to/parent on a CentOS 7 server. How can I compile it so that the resulting .class or .jar file can be called succesfully from /path/to/parent using the java command? I am trying to leave /path/to/parent as clean as possible without cluttering it with files that can be put in subdirectories. Also note that the program requires many dependencies, which are located in /path/to/parent/dependencies/

Here is what I have so far:

The code for Test.java is:

package somepackage;  

public class Test{
    public static void main(String[] args){
        int index;
        for (index = 0; index < args.length; ++index){
            System.out.println("args[" + index + "]: " + args[index]);
        }
    }
}

When the terminal is pointed to /path/to/parent/ , I type the following to complile Test.java:

javac -d bin -cp .:"/path/to/parent/dependencies/*" Test.java

I am then able to navigate to /path/to/parent/bin and successfully run the program by typing the following:

java -cp .:"/path/to/parent/dependencies/*" somepackage.Test arg0 arg1

However, I am NOT able to run the program from /path/to/parent because attempting to do so gives the error: Could not find or load main class somepackage.Test. So how can I change the above so that I can call the program successfully from /path/to/parent using the java command, and without cluttering /path/to/parent?

Upvotes: 1

Views: 34

Answers (1)

talex
talex

Reputation: 20455

Use java -cp "/path/to/parent/bin":"/path/to/parent/dependencies/*" mainpackage.Test arg0 arg1 to run it.

Upvotes: 1

Related Questions