本文介绍了从终端运行java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前有人问过,但由于某种原因我仍然无法弄清楚什么是错的.我在文件 src 的语法测试包中得到了一个名为 NewClass 的类.从 src 路径我输入:

This question has been asked before but I still cannot figure whats wrong for some reason.I got a class named NewClass in package syntaxtest in file src. From src path I type :

javac src/syntaxtest/NewClass.java

javac src/syntaxtest/NewClass.java

并且类被编译,我可以在syntaxtest文件夹中看到NewClass.class.现在从与 NewClass.class 相同的路径甚至相同的文件夹中,我无法弄清楚如何从终端运行该类.我做了很多不同的尝试,但我得到了

and the class is compiled and I can see NewClass.class in syntaxtest folder. Now from that same path or even the same folder with NewClass.class, I can't figure out how to run the class from terminal. I have made many different attempts but ether I get

ClassDefNotFound 或 ClassDefNotFound(错误名称:syntaxtest/NewClass)

ClassDefNotFound or ClassDefNotFound (wrong name : syntaxtest/NewClass)

推荐答案

尝试java -cp src syntaxtest.NewClass".

也就是说,如果您有一个文件夹src",其中包含子文件夹(包)syntaxtest"并且类NewClass"在包语法测试"中,那么上述命令将起作用.

That is, if you have a folder "src" which contains the subfolder (package) "syntaxtest" and the class "NewClass" is in "package syntaxtest", then the above command will work.

$ ls src/syntaxtest
NewClass.java
$ cat src/syntaxtest/NewClass.java
package syntaxtest;
public class NewClass {
  public static void main(String args[]) {
    System.out.println("Hello, World!");
  }
}
$ javac src/syntaxtest/NewClass.java
$ java -cp src syntaxtest.NewClass
Hello, World!

这篇关于从终端运行java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:44