• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make this packages example program work

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

com and foo folder gets created in pe folder now



what is pe folder?
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pe folder is the folder in which i saved MyClass.java
and Another.java
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it might be classpath issue.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

they had mentioned either one will work
so i kept just one
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Congrats Now your program is running
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic