This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Not able to COMPILE!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to COMPILE!!!" Watch "Not able to COMPILE!!!" New topic
Author

Not able to COMPILE!!!

Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
vijayreddy patil
Greenhorn

Joined: Feb 12, 2008
Posts: 5
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

Joined: Feb 12, 2008
Posts: 5
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 22, 2008
Posts: 135
thnx a lot..i got it.i didnt set the classpath thats why getting error
 
 
subject: Not able to COMPILE!!!
 
Similar Threads
Access modifiers
import mypack.*; (asterisk and without asterisk)
Import
Access modifier question
Javac error