Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to COMPILE!!!

 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!!

i have two java file A.java and B.java as
A.java contains

public class A{

A(){System.out.println("In class A");}
}
and B.java contains

public class B {
public static void main(String[] args){

A obj1=new A();
}
}

both are placed in directory C
if i m compiling the A.java by C:\>javac A.java it works fine and A.class file created in directory C.

Now if i m compiling B.java by C:\>javac B.java it is showing error like can not find symbol A.

Also if we create only single file B.java as

class A{

A(){System.out.println("In class A");}
}



public class B {
public static void main(String[] args){

A obj1=new A();
}
}

and compiling by C:\>B.java it works fine with two .class (A.class and B.class)file created.

Can you help me to figure out What is the problem with first approach??

Thanks in advance

Prabhat
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like your classpath is not set up correctly.
Moving to Java In General (Beginner) where classpath issues are covered.
If you're in a hurry for the answer, check out the Java in General FAQ entries on setting up the classpath.
 
vijayreddy patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you try this one


class A{

A(){System.out.println("In class A");}
}
and B.java contains

public class B {
public static void main(String[] args){

A obj1=new A();
}
}
 
vijayreddy patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one class that is a class with main() method can declared as a public...

It should works

class A{

A()
{
System.out.println("In class A");
}
}


public class B
{
public static void main(String[] args)
{

A obj1=new A();
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayreddy patil:
Only one class that is a class with main() method can declared as a public...



Each source file may contain no more than one public top-level class or interface, but this has nothing to do with the main method.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When B.java compiles, it needs to be able to find A.class.

As Ben suggested, this sounds like a classpath issue. My guess is that you have set a system classpath that does not contain a dot (.) for the current directory. Try compiling with the classpath flag...

C:\>javac -cp . B.java

Note the spaces around the dot. If this works, then you need to either remove your system classpath or at least make sure it contains a dot.
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx a lot..i got it.i didnt set the classpath thats why getting error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic