Don't ask why:-) Just because this is interesting for me.
I'm able to run simple programs from commandline but only if they are written into one single file. It works fine.
For example, if the CatTest.java file is inside the C:\TEST folder, then i run it from the commandline this way:
C:\TEST> javac.CatTest.java // and Enter, and:
C:\TEST> java CatTest // Enter, and the commandline will print the output.
Here is this CatTest.java file:
public class CatTest{
static class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
Cat cat = new Cat("Simba");
System.out.println("Catname= " + cat.getName());
}
}
But when i tried to split this program into two different files and put into one package, it wasn't work.
Here is how i did it:
I created a Cat.java file:
package CatTest;
public class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
and an other file with name Main.java :
package CatTest;
public class Main {
public static void main(String[] args) {
Cat cat = new Cat("Simba");
System.out.println("The name of the cat: " + cat.getName());
}
}
and i was put these two files into a new folder: C:\TEST\src\CatTest
I tried to run it this way:
C:\TESZT\src\CatTest> javac Main.java
but i got this compiler-error: "cannot find symbol Cat"
I changed the line 4 in Main class to this one:
CatTest.Cat cat = new CatTest.Cat("Simba");
but the error is the same: "cannot find symbol Cat".
But why? The package name and the folder name is the same.
What else is missing to work well?