So I've been trying to solve this problem for a matter of hours now. I've scoured the internet, I've scoured StackOverflow, I've asked some co-workers (I'm an intern) and honestly no one can tell me what is going on! I put together a really really simple example to show you what I'm doing (and I get the error even with the simple example)
I have two .java
files. One is Test.java
the other is testClass.java
.
//testClass.java
package test;
public class testClass {
private int someMember=0;
public testClass(){
//kill me now
}
}
Then I have my Test.java file which contains my main method. (although in my real problemIi dont have a main method - its a servlet with a doGet()
method)
//Test.java
package test;
public class Test {
public static void main(String[] args) {
testClass myTest = new testClass();
}
}
I'm compiling with the following (from windows command line, with current directory where I saved my .java files):
..java bin location..\javac testClass.java
This works absolutely fine and I get a testClass.class file in the current directory. I, then, try to compile the Test.java file with the following (again within the working directory):
..java bin location..\javac -classpath . Test.java
This results in the following error:
Test.java:6: cannot find symbol
symbol : class testClass
location : class test.testClass
testClass myTest = new testClass();
Can you please help a brother out? :(
I did exactly what you did
Then compiled
Then ran it. And it all worked. So this tells me the problem is the way you are compiling. Have you tried compiling both classes together?
Edit - I did put all my files in
test/
dir as Jon Skeet has said. Maybe that's what is different.Your classes are in a package, and Java will look for classes assuming that package structure - but javac won't build that structure for you unless you tell it to; it will normally put the class file alongside the Java file.
Options:
test
directory, and compiletest\Test.java
andtest\testClass.java
-d .
when you compile, to force javac to build a package structure.Using an IDE (Eclipse, IntelliJ etc) tends to encourage or even force you to put the files in the right directory, and typically makes building code easier too.
Easiest fix: Create a directory
test
and place your.java
's in there, add the folder that contains yourtest
-folder into the classpath. If you dont know how to do that just place yourtest
-folder in your java-folder's subfolderlib
(e.g. c:\prog\javasdk\lib). Just compile withjavac Test.java
(testClass will compile automatically), run it withjava test.Test
from anywhere.