简体   繁体   English

从命令行运行Java程序

[英]Running Java program from commandline

I am trying to run a java program from command line. 我正在尝试从命令行运行Java程序。 I tried following the steps mentioned here . 我尝试按照此处提到的步骤进行操作。 But when I try to run javac Hello.java , it's throwing error that such a program is not there. 但是,当我尝试运行javac Hello.java ,抛出了这样的程序不存在的错误。 I tried giving java Hello.java and got the error: 我试图给java Hello.java并得到错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Hello/java
Caused by: java.lang.ClassNotFoundException: Hello.java
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Hello.java.  Program will exit.

What is the problem here. 这里有什么问题。 How can I do it? 我该怎么做?

EDIT: I have many classes in my code file, Hello.java. 编辑:我的代码文件Hello.java中有很多类。 Will that cause any problem? 这会引起任何问题吗?

First you should compile the java code with 首先,您应该使用

 javac Hello.java

Then run it 然后运行

 java Hello

In both cases, make sure your classpath is set correctly... 在这两种情况下,请确保您的类路径设置正确...

To run the program you need to do: 要运行该程序,您需要执行以下操作:

java Hello

which is java followed by the class name without extension . 这是java后跟没有扩展名的类名。

First off, java requires at most one public class per file. 首先,java每个文件最多需要一个公共类。 No 没有

public class this {
}

public class that {
}

You can have 你可以有

class this {
   class that {
   }
}

if you need. 如果你需要。

EDIT or in file this.java: 编辑或在文件this.java中:

public class this {
}

class that {
}

javac won't be in the jre folder. javac将不在jre文件夹中。 Have you installed the jdk? 您安装了jdk吗? it doesn't come by default on many computers. 默认情况下,许多计算机上都不提供此功能。 it's often in "C:\\Program Files\\Java\\jdk1.6.0_05\\bin\\javac.exe" or a similiar path. 它通常位于“ C:\\ Program Files \\ Java \\ jdk1.6.0_05 \\ bin \\ javac.exe”或类似路径中。

As stated by the others answer, first, you have to run your application using java Hello and not java Hello.java 如其他答案所述,首先,您必须使用java Hello而不是java Hello.java运行应用程序

Second, you have to check that your CLASSPATH is correctly set. 其次,您必须检查您的CLASSPATH是否正确设置。 It seems that your variable is not set or does not integrate the current directory, ie . 似乎您的变量未设置或未集成当前目录,即.

So run : 因此运行:

javac -classpath . Hello.java
java -classpath Hello

or 要么

set CLASSPATH=.
javac Hello.java
java Hello

Of course, defining the CLASSPATH as a user / system variable in your Windows system is a better solution! 当然,在Windows系统中将CLASSPATH定义为用户/系统变量是一个更好的解决方案!

In case Hello.java is contained in a package, you will have to create an appropriate directory structure. 如果Hello.java包含在包中,则必须创建一个适当的目录结构。 Ie in case Hello.java is contained in the package com.stackoverflow , you must create the folders com/stackoverflow and put Hello.java in this folder. 也就是说,如果Hello.java包含在com.stackoverflow包中,则必须创建文件夹com/stackoverflow并将Hello.java放入此文件夹中。 From the root folder you must then launch 然后必须从根文件夹启动

java com.stackoverflow.Hello

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

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