Problem compiling this code

R

rajesh.shiggaon

Hi all:-

I know this question seems very basic, but i am really pissed out, so
please help me out

Code:
package somepackage;
public class aClass {


public static void method(){
System.out.println("Inside the aClass and method");
}


}

I have saved this file in aClass.java and its compiling and placed the
class file in the somepackage directory.

Code:
import somepackage.*;


public class pTest {


public static void main(String args[]){
System.out.println("Inside the main method");
aClass.method();
}


}

this class is saved in the pTest.java file and saved in the parent
directory of somepackage. But when i compile this its giving me error
such as:-

pTest.java:7: cannot access aClass
bad class file: ./aClass.java
file does not contain class aClass
Please remove or make sure it appears in the correct subdirectory of
the classpath.
aClass.method();
^
1 error


When i replace the import statement with this one this one import
somepackage.aClass; its compiling and running.

Am i missing out something ???

Thanks in advance.

Rajesh s
 
K

Kroll, Michael

Hello,

sorry but when I run you code on my machine, there is no error.
It work's fine.
Wich OS and Javaversion do you use ??
I have tested it with Java-Version 1.4.



Michael
 
F

Ferenc Hechler

Hi Rajesh ,

I am not sure if it is ok to have pTest.class in the root-dir (default
package without any package declaration).
Try putting pTest into an package.

Best regards,
feri
 
T

Thomas Weidenfeller

pTest.java:7: cannot access aClass
bad class file: ./aClass.java
file does not contain class aClass

You are trying to run the java source code, instead of the class file.
Why? I can only guess, maybe you packed the wrong file, with the wrong
contents into your jar?

/Thomas
 
B

Bjorn Abelli

Hi all:-

I know this question seems very basic, but i am really
pissed out, so please help me out

[snipped code]
When i replace the import statement with this one this
one import somepackage.aClass; its compiling and running.

Am i missing out something ???

Unfortunately the JVM searches not only for class-files, but also
java-files.

As you are in a root-directory where a aClass.java exists, it "recompiles"
it "believeing" that the class aClass referenced in your code is in the
"default package". As the recompiled aClass doesn't belong to the default
package, it will shout "bad class"...

As you noticed it will work when you import "somepackage.aClass" explicitly.
It will also work with the following call:

somepackage.aClass.method();

....as that also tells the compiler that aClass explicitly exist in package
somepackage, and not in the default package.

Hence it's good practice to separate source code (java-files) from the
compiled code (class-files) in a way that the java-files does *not* exist in
your default path! (a.k.a. using the -d switch when compiling, and
the -classpath switch explicitly when running).

There are of course exceptions from this rule, but it would help in this
specific case.

Just to illustrate it, you can do the following:

- Compile your aClass.java

This creates the aClass.class in the somepackage directory.

- Move aClass.java to another directory, or rename it temporarily

- Compile and run pTest

Now it won't find aClass.java when you compile pTest, and hence it will
work...

// Bjorn A
 
R

rajesh.shiggaon

Thanks Bjorn,

I have a clear idea now. Thanks everyone for your contribution

Bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Void problem 1
How to fix this code? 1
Help in hangman game 1
Trouble with code 2
School Project 1
Problem compiling java programs 6
How do I make this craftinfsystem Work 1
Java matrix problem 3

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top