• Post Reply Bookmark Topic Watch Topic
  • New Topic

Command line executing problem  RSS feed

 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've work till now with IDE and now, i try to do something really simple via command line, and....stupid situation...

I have: d:\foodpackage\fruitpackage

Inside the fruitpackage i have Food.class:



and FoodMain.class


I do this from cmd:
D:\ java foodpackage\fruitpackage\FoodMain

... and i get ClassDefNotFoundException ....

What's the problem?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why you shouldn't start off with IDEs.
you should have FoodMain inside a fruitpackage folder inside a foodpackage folder, and you need to start from outside the foodpackage folder and invoke it with java foodpackage.fruitpackage.FoodMain Use dots not backslashes.

I presume you have got all the classes to compile? It is rather more complicated compiling them. I am going out now, so I shan't have time to explain the different ways to compile inside packages, but that has been done before (not the last two or three weeks) so you should find something useful if you do a search through this beginner's forum.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thank you very much!

I've done how you suggested and it's ok! I've compiled them without problems: D:\ javac foodpackage\fruitpackage\*.java
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great. It would be a lot better if you can UseCodeTags
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehhh... i've got another problem

I've got Hello.class in the same D:\foodpackage\fruitpackage directory



When i execute it like this:
D:\ java foodpackage\fruitpackage\Hello

it doesn't work!!

If i do this:

D:\foodpackage\fruitpackage java Hello

it runs ok

What the heck?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this question involves a very big understanding of the "basics".

The facts involved in your program.

1. Your Hello.java (and Hello.class) does NOT belong to any package. So, it will perfectly run when you just execute "java Hello" inside the directory in which your Hello.class resides. In your case, you did it inside the "D:\foodpackage\fruitpackage".

2. When you did the same in your D: drive, it did NOT find the corresponding "foodpackage\fruitpackage\Hello.class". If you intended to run a java program with its package name, you should have given it as with '.' as provided your Hello.java program belongs to the package "foodpackage.fruitpackage" through the statement



Hope this helps and explains!
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing this: D:\ java foodpackage\fruitpackage\Hello i did not intend to show that we have packages. I just showed the path to the Hello class and that's all. Can't i do that, or i need classpath? I mean, can't i write java [full path to the class]?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vadim Vararu wrote:Doing this: D:\ java foodpackage\fruitpackage\Hello i did not intend to show that we have packages. I just showed the path to the Hello class and that's all.


That's great indeed. If you have understood what you were doing, no issues at all.

Can't i do that, or i need classpath?


You have to have the classpath for locating the .class file you want the 'java' executable to pick up and run. It was set already thence you did NOT get the ClassDefNotFoundError this time.

I mean, can't i write java [full path to the class]?


You can very well do it. But the full path to the class should be in java's perspective -- as a package hierarchy separated with dots (.) and NOT with the Operating System's perspective -- a package hierarchy separated with a slash (/).

Because, its "java" which executes your java program and NOT the underlying OS (windows, unix for example).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code. Note that you can exit your own posts by clicking the edit button to add the code tags if you've forgotten.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Note that you can exit your own posts by clicking the edit button to add the code tags if you've forgotten.


I guess, Jesper meant to say, to 'edit' your own posts, you can do so.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!