• 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:

Run from cmd a Java program that contains other packages

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

this is my first post to Coderanch !!

I am having a problem. I know how to compile and run Java programs from cmd successfully.
I am having a problem with the following:
I have two folders named : packagea and packageb
In package a I have a class named ClassA.java


and in packageb I have a class named ClassB.java


I compiled ClassA with success and I compiled and run ClassB successfully from the command line.
When I alter ClassB like bellow:

It cannot run. It throughs a class not found exception
What am I doing wrong???
 
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
What's the exact and entire error message?  What directory are you running from?  When I'm in the directory that contains packagea and packageb and execute

I get "Got it".  I believe this is because the CLASSPATH contains "." by default.  Maybe try:
 
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you setting the CLASSPATH at all? I have never needed a CLASSPATH for such a simple application. Please confirm that the first package is in a folder called packageA and similarly for packageB. Navigate to a folder where the dir/cpls command shows both packages in its output, i.e. the parent folder for both packages. Now try compiling and executing it with
javac packageA/ClassA.java
javac packageB/ClassB.java
java packageB.ClassB


...and welcome to the Ranch

[Edit;]Not cp; that was a mistake. Use dir on Windows® and ls on *nix.
 
Ioanna Katsanou
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

this is the detailed case:

I have a folder named "folder", which is located on the dekstop of my computer.
So the full path would be C:\Users\Ioanna\Desktop\folder.

Inside that folder I have created a folder which I named packageA. so the path to this would be C:\Users\Ioanna\Desktop\folder\packageA.
I also have another folder which I named packageB. so the path to this would be C:\Users\Ioanna\Desktop\folder\packageB.

Inside packageA, I have a .java file, called ClassA.java. Which is the following:


Inside packageB, I have a .java file, calles ClassB.java:



In Windows cmd I type:



and it compiles and creates the ClassA.class successfully.

Then I type:




and it compiles and creates the ClassA.class successfully.

(I could also do and the same thing for ClassB)


Then, I type

and I get:
Error: could not find or load main ClassB.

When I modify the code of ClassB to this:


and recompile ClassB and run it, I get
"Got it"
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That last classpath is wrong.
You don't need it at all, since the root for your classes is "folder" and the java command defaults to '.' if you don't give a classpath, which in this case would be correct.
 
Ioanna Katsanou
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that seems to work !!! thank you !!


I returned the classB as I had it before , compiled it and typed:

C:\Users\Ioanna\Desktop\folder>java package.ClassB

and it ran !!

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