Reputation: 393
Just today I noticed that I can run java in eclipse with no problems but when I try to run it in the command prompt, I get "cannot find or load main class." The command prompt actually compiles all right, and it outputs a .class file, but then it displays the error msg when trying to execute. (Also, I was able to run java in the cmd a couple weeks ago.)
/* work area for practice
*
*/
package Scrap;
public class experimentational {
public static void main (String [] args) {
System.out.println("welcome to java!");
}
}
Upvotes: 4
Views: 12246
Reputation: 1
I had a similar issue when I copy pasted code into an editor. I removed the package declaration on line 1 and it ran then. So I'd investigate above comments on packages, after trying first to remove the package line.
Upvotes: -2
Reputation: 393
Found the answer: (i'm using different code but it is still relevant to this problem)
java -cp . hiThere
output: "Hi there"
I know this is classpath but don't know why it works or what the period does for it. Anyone have an idea?
Upvotes: 2
Reputation: 993
Did you install a JDK on the machine outside of Eclipse? If you did, then make sure you set your path variables correctly. Open a command prompt (assuming windows) and type java -version
If the JDK was installed properly and path variables were set properly it should tell you the version of Java that was installed. If it tells you that 'java' is not recognized as a command that you do not have a JDK installed, or it was not installed properly.
The reason your program runs in Eclipse is that Eclipse for Java has its own internal JDK and JVM.
Your other option is to set up your path variables to point to Eclispe's internal JDK.
If you were able to run it from a command prompt previously then most likely your class path was altered. Is this a machine at work? Some companies have SMS tasks that come through periodically and restore default system settings (including path variables) to corporate defaults.
Upvotes: 2
Reputation: 2285
Maybe java and javac isn't in your OS path. If you are using Microsoft Windows in cmd type path and then enter. If jdk or jre isn't in path you need to put them to it
Upvotes: 0
Reputation: 2233
Use:
javac Scrap/experimentational.java
followed by:
java Scrap.experimentational
Upvotes: 1