简体   繁体   English

如何在命令行中运行Java程序?

[英]How to run a java program in command line?

Here is the thing: I am trying to run the example program in the joda-time project. 事情是这样的:我正在尝试在joda-time项目中运行示例程序。 The start of the Examples.java file looks like this: Examples.java文件的开始看起来像这样:

package org.joda.example.time;

import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.Instant;

/**
 * Example code demonstrating how to use Joda-Time.
 *
 * @author Stephen Colebourne
 */
public class Examples {

public static void main(String[] args) throws Exception {
    try {
        new Examples().run();
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}

And all the classes for compiling this Example.java is in a joda-time-2.3.jar. 并且所有用于编译Example.java的类都位于joda-time-2.3.jar中。 I can successfully compile this program by using 我可以通过使用以下命令成功编译该程序

    javac -cp somewhere/joda-time-2.3.jar Example.java

And it generate an Example.class, but I jut cannot execute that. 它生成了Example.class,但是我无法执行。 So far I have tried: 到目前为止,我已经尝试过:

java Examples
java -cp somewhere/joda-time-2.3.jar Examples
java -cp somewhere/joda-time-2.3.jar org.joda.example.time.Examples

But they all generate this kind of errors: 但是它们都会产生这种错误:

Error: Could not find or load main class org.joda.example.time.Example 错误:找不到或加载主类org.joda.example.time.Example

Error: Could not find or load main class Examples 错误:找不到或加载主类示例

And I've tried both in the org/joda/example/time folder and the parent folder of org 我在org / joda / example / time文件夹和org的父文件夹中都尝试过

Anyone can give an instruction on how to execute that? 任何人都可以给出有关如何执行该指令的指示吗? Really appreciate it! 真的很感激!

Error: Could not find or load main class org.joda.example.time.Example 错误:找不到或加载主类org.joda.example.time.Example

public class Examples {

Name of your class is Examples not Example 您的班级名称是“ Examples而不是“ Example

EDIT 编辑

Sorry for late reply... 抱歉回复晚了...

To execute specific Java program you need to bring control to root directory so if your class is in abc/newdir/Examples.java you need to use cd command (in windows) to lead control to root directory and than compile or you can defeneitly go for the suggestion of kogut . 要执行特定的Java程序,您需要将控制权移至根目录,因此,如果您的类位于abc/newdir/Examples.java ,则需要使用cd命令(在Windows中)将控制权引导至根目录,然后进行编译,否则您可以进行编译对于kogut的建议。

C:/abc/newdir>java -cp somewhere/joda-time-2.3.jar Examples

Modify your classpath parameter, so it should include directory where Example.class was generated. 修改您的classpath参数,因此它应该包括生成Example.class的目录。

In case of out/org/joda/example/time/Example.class you need to use 如果是out / org / joda / example / time / Example.class,则需要使用

java -cp somewhere/jodata-time-2.3.jar:out org.joda.example.time.Example

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

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