我正在尝试运行一个非常简单的程序并遇到错误。我制作了一个非常简单的 helloworld java 程序,但无法从 Mac 上的终端运行它。我检查以确保类路径设置正确,并且对它为什么不运行感到困惑。
这是程序:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
这是终端命令:
Last login: Thu May 2 12:01:50 on ttys000
172-26-125-179:~ rohan$ cd /Users/rohan/Desktop
172-26-125-179:Desktop rohan$ ls
Hello.java
172-26-125-179:Desktop rohan$ echo $CLASSPATH
172-26-125-179:Desktop rohan$ export CLASSPATH=/Users/rohan/Desktop
172-26-125-179:Desktop rohan$ echo $CLASSPATH
/Users/rohan/Desktop
172-26-125-179:Desktop rohan$ 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(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
172-26-125-179:Desktop rohan$
任何想法将不胜感激! 更新忽略前面的情况,这是对我的问题的更好看法:
Last login: Thu May 2 13:05:17 on ttys000
172-26-125-179:~ rohan$ cd /Users/rohan/Dropbox/Current/ds/lab6
172-26-125-179:lab6 rohan$ ls
Hello.java
172-26-125-179:lab6 rohan$ cat Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("hi");
}
}
172-26-125-179:lab6 rohan$ echo $CLASSPATH
172-26-125-179:lab6 rohan$ export CLASSPATH=/Users/rohan/Dropbox/Current/ds/lab6
172-26-125-179:lab6 rohan$ echo $CLASSPATH
/Users/rohan/Dropbox/Current/ds/lab6
172-26-125-179:lab6 rohan$ javac Hello.java
172-26-125-179:lab6 rohan$ java Hello.class
Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class
Caused by: java.lang.ClassNotFoundException: Hello.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
172-26-125-179:lab6 rohan$