• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class instantiation

 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created one file TestAB.java in a directory, this file has two classes TestA and TestAB. class TestAB has the main method where I create the instance of TestA. TestAB.java compiles successfully and runs as expected.
Now, in another directory I have saved class TestA and class Test AB in two different files TestA.java and Test AB.java, TestA.java compiles successfully but when I compile TestAB.java I get the following error:

TestAB.java:5: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
TestAB.java:5: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
2 errors

Why is the compiler giving these errors? I thank you in advance for your help with this.

// Code
/* TestAB.java */
In a different folder
/* TestA.java *//* TestAB.java */
 
Winston Gutkowski
Bartender
Pie
Posts: 10506
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshuman Shukla wrote:I have created...

Anshuman.

I've added code tags to your post. See how much better it is to read?
Please read the UseCodeTags (←click) page thoroughly.

You can also see what I did by clicking on the 'Edit' icon.

Thanks.

Winston
 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,
My post has become more readable after applying the tags. How were you able to edit my post?
Anshuman.
 
Winston Gutkowski
Bartender
Pie
Posts: 10506
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshuman Shukla wrote:My post has become more readable after applying the tags. How were you able to edit my post?

Ah, tho' but a lowly Bartender, I have certain mystical God-like powers...

Hopefully, I won't need to next time.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Anshuman Shukla!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you compiling your code; on the command line, using javac?

You probably have the CLASSPATH environment variable set. Unset it, so that Java takes the current directory "." as the classpath. Or specify the classpath yourself when you compile your code:

javac -cp . TestAB.java

Then run it:

java -cp . TestAB

Some people here on the forums, for example Winston and myself, are "bartenders" (you can see that under our name on the left side). Bartenders are moderators for the forums: we keep an eye on what happens on the forums and we have the ability to edit other people's posts.
 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

I was half expecting the reply that I got from Winston, thanks for clarifying that 'Bartenders' are moderators. To answer your question, on the command prompt I type javac filename.java to compile my program and java -cp . filename to run it. I tried your recommendation of typing javac -cp . filename.java and my program compiled without errors and it runs as expected.
Now, I tried to insert an additional statement, package Example; in both the files TestAB.java and TestA.java, and tried compiling with javac -cp . Test AB.java, I am getting the same errors

TestAB.java:7: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
TestAB.java:7: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
2 errors

while TestA.java compiles with no errors. What could be the problem?

Also, I just checked that the CLASSPATH environment variable it is set to %JAVA_HOME%|jre|lib .

Thanks,
Anshuman.
 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic