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;
}
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.
Thank you! Surely there must be some tool to do like if i had 7 files instead of two would i necessarily have to type javac 1.java 2.jaava 3 .java... etc.?
Last time I was trying to figure it out I came to conclusion that yes, you have to type each file. But I can be wrong.