1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you and +1 for providing a correct answer quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.