简体   繁体   English

java在eclipse中运行但不会在命令提示符下运行

[英]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." 就在今天,我注意到我可以在eclipse中运行java而没有任何问题,但是当我尝试在命令提示符下运行它时,我得到“无法找到或加载主类”。 The command prompt actually compiles all right, and it outputs a .class file, but then it displays the error msg when trying to execute. 命令提示符实际上编译正确,它输出.class文件,但它在尝试执行时显示错误消息。 (Also, I was able to run java in the cmd a couple weeks ago.) (另外,我几周前能够在cmd中运行java。)

/* 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. 我知道这是classpath,但不知道它为什么会起作用或者它的周期是什么。 Anyone have an idea? 有人有想法吗?

try java -cp . [your main class] 试试java -cp . [your main class] 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. 也许java和javac不在您的操作系统路径中。 If you are using Microsoft Windows in cmd type path and then enter. 如果您在cmd类型路径中使用Microsoft Windows,然后输入。 If jdk or jre isn't in path you need to put them to it 如果jdk或jre不在路径中,则需要将它们放入其中

Did you install a JDK on the machine outside of Eclipse? 您是否在Eclipse之外的机器上安装了JDK? If you did, then make sure you set your path variables correctly. 如果您这样做,请确保正确设置路径变量。 Open a command prompt (assuming windows) and type java -version 打开命令提示符(假设是windows)并输入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. 如果正确安装了JDK并且路径变量设置正确,它应该告诉您已安装的Java版本。 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. 如果它告诉您“java”未被识别为未安装JDK的命令,或者未正确安装。

The reason your program runs in Eclipse is that Eclipse for Java has its own internal JDK and JVM. 程序在Eclipse中运行的原因是Eclipse for Java有自己的内部JDK和JVM。

Your other option is to set up your path variables to point to Eclispe's internal JDK. 您的另一个选择是将路径变量设置为指向Eclispe的内部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. 有些公司会定期执行SMS任务,并将默认系统设置(包括路径变量)恢复为公司默认值。

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. 我删除了第1行的包声明然后运行了。 So I'd investigate above comments on packages, after trying first to remove the package line. 因此,在首先尝试删除包装行后,我会调查上面对包装的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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