GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to make this packages example program work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make this packages example program work" Watch "How to make this packages example program work" New topic
Author

How to make this packages example program work

Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52


Should MyClass.java and Another.java be in different folders or in the same folder?
Do I have to create com and foo folders?

Thank you
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Should MyClass.java and Another.java be in different folders or in the same folder?


There is no need MyClass.java and Another.java is in same folder (But you can place them in same folder )because MyClass and its method is public.

Do I have to create com and foo folders?


You don't need to create any folder it will be created automatically when you compile your program.


Life is easy because we write the source code.....
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
I get an error when I put MyClass.java and Another.java in same folder

MyClass.java compiles without error
and a MyClass.class file is created in pe folfer

I get error on compiling Another.java

F:\pe>javac Another.java
Another.java:1: package com.foo does not exist
import com.foo.*;
^
Another.java:9: cannot access MyClass
bad class file: .\MyClass.class
class file contains wrong class: com.foo.MyClass
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
MyClass m1=new MyClass();
^
2 errors
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Make sure that MyClass.class file present in com.foo folder.

Compile MyClass.java program as

then compile Another.java program as

Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
com and foo folder gets created in pe folder now

but I still get an error

F:\pe>javac Another.java
Another.java:9: cannot access MyClass
bad class file: .\MyClass.class
class file contains wrong class: com.foo.MyClass
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
MyClass m1=new MyClass();
^
1 error
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

com and foo folder gets created in pe folder now


what is pe folder?
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
pe folder is the folder in which i saved MyClass.java
and Another.java
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Ok , Then compile MyClass.java file once again as Here I have pe under 'C' drive Because of this under pe folder com folder is created and under com foo folder is created and in foo MyClass.class file is created. If it's then compile Another.java program as
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
under pe folder com folder is created and under com foo folder is created and in foo MyClass.class file is created.

but when I compile Another.java
pe>javac Another.java

I get an error



pe>javac Another.java
Another.java:9: cannot access MyClass
bad class file: .\MyClass.java
file does not contain class MyClass
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
MyClass m1=new MyClass();
^
1 error
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I think it might be classpath issue.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You should keep your sources in their respective package.
1. Create a directory called "com" in your "pe" directory
2. Create a directory called "foo" in the "com" directory
3. Move your java files in the F:\pe\com\foo
4. Try javac -cp . com\foo\MyClass.java


[My Blog]
All roads lead to JavaRanch
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
It's working

I had to add
import com.foo.MyClass;

to Another.java

only import com.foo.*; was not enough


thanks
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

But in your first post you have mentioned that you have added that import statement as
I think you have removed that one later therefore it gives you error.
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52

they had mentioned either one will work
so i kept just one
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Ok, Congrats Now your program is running
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52

Thanks

How to make this program work

if Another.java is placed directly in F:>

F:> javac Another.java
gives errors --6 errors

F:\>javac -classpath pe/com/foo Another.java

Also gives errors

Another.java:1: package com.foo does not exist
import com.foo.MyClass;
^
Another.java:2: package com.foo does not exist
import com.foo.*;
^
Another.java:10: cannot access MyClass
bad class file: pe\com\foo\MyClass.class
class file contains wrong class: com.foo.MyClass
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
MyClass m1=new MyClass();
^
3 errors
Vijay Tyagi
Ranch Hand

Joined: Feb 15, 2010
Posts: 52
OK

when i place Another.java

in F:> directly

and MyClass.java where it was ,i.e in F:\pe folder

Another.java compiles with this command
F:> javac -cp pe/ Another.java

But it doesn't execute, how do i make it run ?

F:\>java -cp pe/. Another
Exception in thread "main" java.lang.NoClassDefFoundError: Another
Caused by: java.lang.ClassNotFoundException: Another
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Another. Program will exit.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because you can't run a source file, only a class file. And when you execute a class file, you leave off the ".class" filename suffix.
 
GeeCON Prague 2014
 
subject: How to make this packages example program work