• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question about javac and java commands

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

I noticed something regarding the java command that I wanted to ask about. I created a temp folder on the root of C:\ then I created two more folders called packagea and packageb. In those folders were java files called ClassA and ClassB respectively. I compiled them by running javac packagea/ClassA.java packageb/ClassB.java

So then I tested to make sure it worked by running java packageb.Classb - which is where I coded the main method - and it worked. OK, good so far.

But then..

I changed my working directory to C:\temp\packageb and then attempted to run java ClassB and that does not work. I get:

Error: Could not find or load main class ClassB

Could anyone explain what is happening here?
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would need to see the code to be sure, but you probably have a package statement at the beginning of your program. That means you have to run the program from the "root" of the project or else Java can't find the class.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ClassB


ClassA
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package packageB; says "look in the folder packageB" for the class. import packageA.ClassA; says "you can find ClassA in the folder packageA". So you need to launch the program from a folder that can see both packageA and pacakgeB, or you need to have a classpath entry to each folder.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:package packageB; says "look in the folder packageB" for the class. import packageA.ClassA; says "you can find ClassA in the folder packageA". So you need to launch the program from a folder that can see both packageA and pacakgeB, or you need to have a classpath entry to each folder.



Thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic