0

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.

1
  • 1
    You need to import com.test.*; or (and this is what you're going for, I think) import static com.test.Print.*;. This isn't what your question is about obviously, it's just something you'll need to change to get it working later :) If you use the static import, then you can replace Print.print(...) with print(...). Commented Aug 3, 2017 at 21:26

1 Answer 1

2

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.

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

Comments

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.