Warm tip: This article is reproduced from serverfault.com, please click

How to access other files in any editor?

发布于 2020-12-11 02:08:05

For example I have Main.java and test.java. test has public static int bro = 5; so i try to print test.bro from Main but the class test is not found. An IDE like Eclipse takes care of this for me but how do I do this with an editor? Sorry noob question. I'm in cmd in the directory of deez files and i type javac Main.java, den java Main. Thanks.

file Main.java:

public class Main {
   public static void main(String[] args) {
      System.out.println(test.bro);
   }
}

file test.java:

public class test {
   public static int bro = 5;
}
        
Questioner
Deqrees LoL
Viewed
0
Юрий Баринов 2020-12-11 11:04:37

So suppose you have two source files: Main.java and test.java then you need to compile them first.

You can do it via command javac Main.java test.java. That command will produce 2 files in your current directory: Main.class and test.class. Which contain compiled java code.

Now you need to run your main class with classpath which contains both of your classes. So you need to run command java -cp . Main. Where . represents directory with your compiled classes.