• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Not able to COMPILE!!!

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx a lot..i got it.i didnt set the classpath thats why getting error
 
Hold that thought. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic