您应该使用jar cfm myApp.jar manifest.txt *.class
来创建 jar,以便清单文件在 jar 中正确定位。
清单的正确位置是META-INF/MANIFEST.MF
.
更新
我已经让你的代码工作了,基本上是通过你准备的文件;将包声明添加到 java 文件和清单,并将 myApp 中的 MyApp 类大写。文件按以下文件夹结构排列:
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ ls -lR
total 0
drwxr-xr-x 3 tbsalling staff 102 19 Jun 18:48 META-INF
drwxr-xr-x 4 tbsalling staff 136 19 Jun 18:57 thank
./META-INF:
total 8
-rw-r--r-- 1 tbsalling staff 46 19 Jun 18:49 MANIFEST.MF
./thank:
total 16
-rw-r--r-- 1 tbsalling staff 124 19 Jun 18:49 MyApp.java
-rw-r--r-- 1 tbsalling staff 98 19 Jun 18:48 T1.java
这三个文件的内容是:
MyApp.java:
package thank;
class MyApp {
public static void main(String args[]) {
T1 t=new T1();
t.display();
}
}
T1.java:
package thank;
class T1 {
void display() {
System.out.println("Hey I am working");
}
}
清单.MF:
Main-Class: thank.MyApp
Manifest-Version: 1.0
然后我运行这一系列命令:
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ javac thank/T1.java thank/MyApp.java
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ jar cfm myApp.jar META-INF/MANIFEST.MF thank/*.class
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ java -jar myApp.jar
Hey I am working
^^^ 它有效;-)