执行来自《Java编程思想》的HelloDate示例。

huangapple 未分类评论44阅读模式
英文:

Execute HelloDate from Thinking in Java

问题

Ubuntu 18.04

我已经下载了《Thinking in Java》的示例代码。将它们解压缩后,现在我需要执行HelloDate。

.
├── operators
│    ├── HelloDate.class
│    ├── HelloDate.java

代码内容:

//: operators/HelloDate.java
import java.util.*;
import static net.mindview.util.Print.*;

public class HelloDate {
  public static void main(String[] args) {
    print("Hello, it's:");
    print(new Date());
  }
} /* Output: (55% match)
Hello, it's:
Wed Oct 05 14:39:05 MDT 2005
*///:~

我按照以下方式编译它:

michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples$ javac operators/HelloDate.java

编译成功,如我们所见。

然后:

michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples$ java operators.HelloDate
错误: 找不到或无法加载主类 operators.HelloDate
原因: java.lang.NoClassDefFoundError: HelloDate (wrong name: operators/HelloDate)

您能帮我解决这个问题吗?首先理解问题,其次解决问题。

英文:

Ubuntu 18.04

I've downloaded examples for "Thinking in Java". Unzipped them. Now I have to execute HelloDate.

.
├── operators
│   ├── HelloDate.class
│   ├── HelloDate.java

The code:

//: operators/HelloDate.java
import java.util.*;
import static net.mindview.util.Print.*;

public class HelloDate {
  public static void main(String[] args) {
    print("Hello, it's: ");
    print(new Date());
  }
} /* Output: (55% match)
Hello, it's:
Wed Oct 05 14:39:05 MDT 2005
*///:~

I compiled it like this:

michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples$ javac operators/HelloDate.java 

The compilation was successful, as we can see.

Then:

michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples$ java operators.HelloDate 
Error: Could not find or load main class operators.HelloDate
Caused by: java.lang.NoClassDefFoundError: HelloDate (wrong name: operators/HelloDate)

Could you help me with this problem. First to understand it, secondly - to solve.

答案1

得分: 0

当您从任何教程下载类似这样的示例时,最好使用诸如Eclipse、Spring Tool Suite或IntelliJ IDEA等Java集成开发环境,并在那里导入项目。

如果您仍然需要从命令行执行,您只需要删除第一行中的“package”名称。
或者如@Sebastian所说,您还可以尝试从“operators”子目录中执行。

javac operators/HelloDate.java
java HelloDate

如果该文件包含任何其他依赖类/库(jar文件),那么您需要将它们添加到类路径并执行,就像这个教程中所示。
例如:

$ javac –classpath C:\dependency\framework.jar HelloDate.Java
$ java –classpath C:\dependency\framework.jar HelloDate
英文:

When you download some ready examples like this from any tutorial, it will be better if you use any Java IDE's like Eclipse, Spring Tool Suite or IntelliJ IDEA and import the project there.

If you still need to execute from command line, all you need to do is, remove the "package" name, which will be in the first line.
Or as @Sebastian said, you can also try executing from "operators" sub directory.

javac operators/HelloDate.java
java HelloDate

And if that file happens to contain any other dependency classes/libraries (jar files), then you need to do all the circus of adding them into classpath and execute, as shown in this tutorial.
Eg:

$ javac –classpath C:\dependency\framework.jar HelloDate.Java
$ java –classpath C:\dependency\framework.jar HelloDate

huangapple
  • 本文由 发表于 2020年4月5日 00:56:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/61031563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定