简体   繁体   中英

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 ?

使用java -cp "/path/to/parent/bin":"/path/to/parent/dependencies/*" mainpackage.Test arg0 arg1来运行它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM