• Post Reply Bookmark Topic Watch Topic
  • New Topic

Run from cmd a Java program that contains other packages  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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???
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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:
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • 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: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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"
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • 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: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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 !!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!