简体   繁体   中英

Adding -classpath from command line

So I have two java files, Print.java and StaticImport.java, in src/com/test.

StaticImport.java:

package com.test;
import static com.test.*;

class StaticImport {
  public static void main(String[] args) {
    System.out.println("Hello world");
    Print.print("This is cool");
  }
}

Print.java:

package com.test;

public class Print {
  public static void Print(String command) {
    System.out.println(command);
  }
}

So basically there is the StaticImport class that uses Print class . How can I compile the StaticImport with javac in command line?
I have tried for example: javac -cp /home/pathToProj/ StaticImport.java , but with no success.

In java, the classpath contains class files, not java code.

First, you need to compile Print.java, since you need it to be on your classpath. Then you need to set the classpath for the compilation of StaticImport to be the directory containing the "com" directory above Print.class.

You can also compile both files at the same time, using a single call to javac .

However, the best thing to do is to use either maven or gradle to build it for you. They look after your classpath, and do a million other things besides.

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