• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why won't my program run through the command line?  RSS feed

 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to learn java using the command line. I successfully compiled my program but can't run it for some reason. Terminal returns whenever I run "java someProgram"
that it "could not find or load main class".

Any thoughts?

A screenshot has been attached. Thanks!
Screen-Shot-2015-06-14-at-8.29.27-PM.png
[Thumbnail for Screen-Shot-2015-06-14-at-8.29.27-PM.png]
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this error is happening because you didn't save your .java file as the same name as the main class file. Make sure the name of your program file is same as the name of the class in which the main method is defined.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you include the source of your compilable java program?

The command look right, but without seeing what the java class is, and what it compiles to it is difficult to help

 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes. Code below:

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your class is in "chap14" package. So first you need to compile it like "javac -d <path where you want to put it> Calculator.java", then you can use it like "java chap14.Calculator".
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than compiling it to a specific place, leave it in this directory and run it from the parent directory as Tushar has said
ie:


 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That approach, to run my program from a parent directory (and not from its package), worked. The question that now comes up: why can't you run a program from inside its package?

Screen-Shot-2015-06-15-at-2.28.43-PM.png
[Thumbnail for Screen-Shot-2015-06-15-at-2.28.43-PM.png]
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the name of the class is packagename.ClassName
You must be able to use that notation to find your class. That also has to match the directory structure. So you must call it from a location where packagename is the name of a directory/folder in the current location.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!