How do I compile and run the following programs:
Test1.java:
package A;
public class Test1
{
public int a = 1;
}
Test2.java:
There's nothing wrong with the way you are compiling, it's just cumbersome but certainly not wrong.
That being said, create a src directory to store your .java files, keeping your directory structure coherent with the package structure of your classes. In this case you would have src directory and inside it, directory A and directory B. Inside A put Test1.java and inside B put Test2.java
Then:
javac B/Test2.java
Why Test2.java? Because it depends on A, then the compiler is smart enough to first compile A/Test1.java and then B/Test2.java. At this point you have each .class files inside A and B
To run it:
java B.Test2