Hi,
I have executed the sample package program and it executes fine in some conditions.I have a doubt in my program .

My directory structure is:/javaprograms/world/HelloWorld.java
A folder name :world
A java filename:HelloWorld.java

package world;
import java.io.*;
import java.util.*;
public class HelloWorld{
        public static void main(String args[])throws IOException{
        System.out.println("Package Program With Example");
        }
        public static String show(){
                return "Hai how are you";
        }
}

This program is executed fine.

In javaprograms folder
A filenamed :ex_Hello
Importing package world and use in the program .It also executes fine.
Because the world folder is inside the javaprograms folder.

import java.io.*;
import world.*;
class ex_Hello

{
        public static void main(String args[])throws IOException{
        String output;
        System.out.println("Import packages from the source folder world");
        output=HelloWorld.show();
        System.out.println("The output of the program is "+output);
}
}

-------------------------------------------------------------------------------
My doubt is when i keep the program ex_Hello.java outside the javaprogram folder and it doesnot work.Eventhough i hava specified the javaprogram folder in the import.
Forex:

import java.io.*;
import javaprograms.world.*;
class ex_Hello

{
        public static void main(String args[])throws IOException{
        String output;
        System.out.println("Import packages from the source folder world");
        output=HelloWorld.show();
        System.out.println("The output of the program is "+output);
}
}

It displays the error like:
ex_Hello.java:9: cannot access HelloWorld
bad class file: ./HelloWorld.java
file does not contain class HelloWorld
Please remove or make sure it appears in the correct subdirectory of the classpath.
output=HelloWorld.show();

Can anyone say why this error appearing ?

Thank you,

With Regards,
Prem

Recommended Answers

All 21 Replies

With the first line of your HelloWorld.java file, you have specified the class to be a part of package 'world':-

package world;

That is why the 'world' folder is identified as a valid package, but 'javaprograms' is not.
If you would like it to be recognized as a valid java package, then you would have to declare your HelloWorld class to be a part of package 'javaprograms.world' as below:-

package javaprograms.world;

i.e. 'javaprograms' would be your parent package, and world its sub-package, then your program would run in the final case.

Hi stephen84s,
I have changed the code now

package javaprograms.world;
import java.io.*;
import java.util.*;
public class HelloWorld{
        public static void main(String args[])throws IOException{
        System.out.println("Package Program With Example");
        }
        public static String show(){
                return "Hai how are you";
        } 
}

In import package file named:ex_Hello.java kept outside of the javaprograms folder.Still, i am getting the error.
filename :ex_Hello.java

import java.io.*;
import javaprograms.world.*;
class ex_Hello

{
	public static void main(String args[])throws IOException{
	String output;
	System.out.println("Import packages from the source folder world");
	output=HelloWorld.show();
	System.out.println("The output of the program is "+output);
}
}

Error :

ex_Hello.java:9: cannot access HelloWorld
bad class file: ./HelloWorld.java
file does not contain class HelloWorld
Please remove or make sure it appears in the correct subdirectory of the classpath.
output=HelloWorld.show();
^

Thank you,

With Regards,
Prem

bad class file: ./HelloWorld.java

Why is it refering to a .java source file as being a bad class file?

Is this an IDE configuration problem?

What command do you execute to generate that error message shown in red?

Hi NormR1,
I just execute a normal exeuction step.I do no anything about the IDE.

Thank you,
Prem

I just execute a normal exeuction step

What is a "normal execution step"?

What command do you execute to generate that error message shown in red?
Please show the full contents of the command prompt screen.

hi,
ex_Hello.java is outside the folder javaprograms.

import java.io.*;
import javaprograms.world.*;
class ex_Hello

{
        public static void main(String args[])throws IOException{
        String output;
        System.out.println("Import packages from the source folder world");
        output=HelloWorld.show();
        System.out.println("The output of the program is "+output);
}
}

javac ex_Hello.java
ex_Hello.java:9: cannot access HelloWorld
bad class file: ./HelloWorld.java
file does not contain class HelloWorld
Please remove or make sure it appears in the correct subdirectory of the classpath.
output=HelloWorld.show();
^
1 error


Thank you,

With Regards,
prem

HelloWorld is in a package: package javaprograms.world;
The classpath must point to the folder containing the javaprograms folder for the javac commmand to find the HelloWord.class file.

Your javac command doesn't appear to have a classpath option.
Is javaprograms a folder in the folder containing ex_Hello.java

Hi,
pg:
HelloWorld.java
path:prem/javaprograms/world/HelloWorld.java
and the class file HelloWorld.class is also in same path.

package javaprograms.world;
import java.io.*;
import java.util.*;
public class HelloWorld{
        public static void main(String args[])throws IOException{
        System.out.println("Package Program With Example");
        }
        public static String show(){
                return "Hai how are you";
        } 
}

The another program which import package is kept in the path
outside of the javaprograms folder:
/prem/ex_Hello.java


javac ex_Hello.java
ex_Hello.java:9: cannot access HelloWorld
bad class file: ./HelloWorld.java
file does not contain class HelloWorld
Please remove or make sure it appears in the correct subdirectory of the classpath.
output=HelloWorld.show();
^
1 error

Thank you,

With Regards,
prem

ex_Hello is not in the same package as HelloWorld.
Where is the source file: ex_Hello.java?

The javac program requires that the folder containing the folder javaprograms be on the classpath. Is it?

May I ask a question? Probably it is stupid but just in case to make sure.

Have you compiled the HelloWorld.java file and where the HelloWorld.class file is?

Hi,
I explained the directory structure clearly,
The folder
prem-> javaprograms->world->HelloWorld.java and HelloWorld.class
Ist Part:
Executing the HelloWorld.java file in the folder prem:
java javaprograms.world.HelloWorld
The result is,
Package Program With Example

The file
ex_Hello.java kept in
prem->ex_Hello.java

The command typed inside the prem folder:
javac ex_Hello.java
It displays the error.


Thank you,

With Regards,
Prem

bad class file: ./HelloWorld.java

I still do not understand why the error message refers to a source file???

I just copied the files to the folders as you describe them and had no problems compiling and executing ex_Hello. Its output:

Import packages from the source folder world
The output of the program is Hai how are you

pls Send the output and execution steps for executing the ex_Hello.java

Thank you,

With Regards,
prem

I copied your HelloWorld.java file to the world folder (in the javaprograms folder) and compiled it.
I copied the ex_Hellow.java file to the folder where the javaprograms folder was.
I javac ex_Hellow.java end then executed it:


D:\JavaDevelopment\Testing\ForumQuestions3>java ex_Hello
Import packages from the source folder world
The output of the program is Hai how are you

D:\JavaDevelopment\Testing\ForumQuestions3>dir javaprograms
Volume in drive D is Work
Volume Serial Number is 1463-F5AE

Directory of D:\JavaDevelopment\Testing\ForumQuestions3\javaprograms

08/17/2010 08:30 AM <DIR> .
08/17/2010 08:30 AM <DIR> ..
08/17/2010 08:36 AM <DIR> world
0 File(s) 0 bytes
3 Dir(s) 8,767,406,080 bytes free

D:\JavaDevelopment\Testing\ForumQuestions3>dir javaprograms\world
Volume in drive D is Work
Volume Serial Number is 1463-F5AE

Directory of D:\JavaDevelopment\Testing\ForumQuestions3\javaprograms\world

08/17/2010 08:36 AM <DIR> .
08/17/2010 08:36 AM <DIR> ..
08/17/2010 08:32 AM 817 HelloWorld.class
08/17/2010 08:32 AM 330 HelloWorld.java
2 File(s) 1,147 bytes
2 Dir(s) 8,767,406,080 bytes free

D:\JavaDevelopment\Testing\ForumQuestions3>

Hi,
Is there any permission issue for the folders and files?I have runned my program as a root user.

Thank you,

With Regards,
Prem

Not on my version of WinXP.

@prem2

After changing the package statement, have you recompiled the HelloWorld.java file ?

Hi NormR1,
I have executed the program successfully With some changes.But it does not satisfy my requirment.


I have put the ex_Hello.java file in the same directory world.

The world directory contains the,
HelloWorld.java

So , both the ex_Hello.java file and world folder are in the same folder.
javaprograms dir.

changes:
In ex_Hello.java

import java.io.*;
import world.*;
class ex_Hello

{
	public static void main(String args[])throws IOException{
	String output;
	System.out.println("Import packages from the source folder world");
	output=HelloWorld.show();
	System.out.println("The output of the program is "+output);
}
}

program: HelloWorld

package world;
import java.io.*;
import java.util.*;
public class HelloWorld{
	public static void main(String args[])throws IOException{
	System.out.println("Package Program With Example");
	}
	public static String show(){
		return "Hai how are you";
	} 
}

1.Is this compulsory to keep the package folder and importing package file in the same directory?
2.If you have any suggestion please specify it?

Thank you,

With Regards,
Prem

Yes, stephan i have recompiled it every time .

Is the classpath set on your PC? Are you using an IDE that sets the classpath?

I built the folder structure you showed, used your sources with the package statements and was able to compile and execute as I posted.

You show two different package statements:
package javaprograms.world;
and
package world;

If you change that, then my tests will not work. I used the first one.

But, when i use the first one i did not get the result.so only i tried the second step.I dont know about the IDE .I am using the fedora redhat.So did not set the classpath .

JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/


Thank you,


With Regards,
Prem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.