• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run a program from the command line  RSS feed

 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, about this post.

Is it possible to execute every program from cmd? Only with the main class? What should I type then?

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have a class with a main method. If it is called Foo in a package called mypackage, you need to go to one folder above the mypackage folder where the .class file is. You write the fully qualified name of the class.
java mypackage.Foo
Don't write .java or .class
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

:-((, it is not working correctly.

I have tried it with the file.java & file.class, I have obtained same error in both cases:

C:\Users\R\Desktop\TPVv1_2\src>Control.Main

C:\Users\R\Desktop\TPVv1_2\src>Control.Main
"Control.Main" no se reconoce como un comando interno o externo, programa o archivo por lotes ejecutable.

C:\Users\R\Desktop\TPVv1_2\build\classes>Control.Main

C:\Users\R\Desktop\TPVv1_2\build\classes>Control.Main
"Control.Main" no se reconoce como un comando interno o externo, programa o archivo por lotes ejecutable.

I have installed again the jdk, & also I have followed the instructions of this webpage:
Java JDK

But I receive the same error in cmd, this is something extrange isn't it?

Thanks.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ruth,

Lets try in a simple way (for the simplicity, remove any packagings statements from your code if you have at the moment, should be the very first statement something like "package program;"):

1. When you write a program, for example with simple text editor, after you finish it, you save your program as "MyProgram.java" (.java is something, that computer, more precise java virtual machine, cannot read and execute).
2. To make it readable, you need to compile that program which you named "MyProgram.java". In console you need to be in the same folder where your program is, and write:
javac MyProgram.java (and hit enter)
3. After that, in the same folder suppose to appear MyProgram.class file (.class is something, that computer, more precise java virtual machine, can read and execute).
4. In order to execute/run your program, you need to be in the same folder where your "MyProgram.class" file is, and write:
java MyProgram (without .class and not "javac", but "java" this time).

You might want to know how to navigate in console:
If you're using windows, then in commander you can go to a folder with a command "dir folder_name/" (go back "dir .."). If non-windows system "cd folder_name/ (go back "cd ..").
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you still don't see what's happening: two people have told you to type "java" followed by the fully-qualified class name. You typed something which looks like a fully-qualified class name, but you didn't type "java" before it.

That blog entry you linked to tells you what to do when "java" isn't a recognized command, namely to install Java. But your problem wasn't that "java" wasn't recognized, it was that your class name wasn't a recognized command. So that link would be useless.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ups! It's true I didn't realize that word "java" was first thing in this sentence.

:-| Sorry!

It's true, now it's working perfectly.

Thank you very much!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!