简体   繁体   中英

java runs in eclipse but won't run in command prompt

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!");



}

}

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?

try java -cp . [your main class] java -cp . [your main class] .

Use:

javac Scrap/experimentational.java

followed by:

java Scrap.experimentational

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM