我编写了一个 java 程序,它使用 jxl.jar 读取 excel 文件。它目前正在工作,但我必须使用 cmd 来运行该程序。双击 jar 文件似乎不起作用。这些是我用来编译和运行代码的命令:
javac -classpath C:/workspace/jxl.jar:. main.java GUi.java
jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class
java -cp run.jar Main
我不太确定为什么它与双击它有什么不同。我已将 jxl 文件编译到 run.jar 文件中,所以我不明白为什么它不起作用?
最佳答案
示例直接来自 Adding Classes to the JAR File's Classpath 上的 Java 教程正如 @MadProgrammer 在他的评论中所建议的。
我们希望将 MyUtils.jar
中的类加载到类路径中,以便在 MyJar.jar
中使用。这两个 JAR 文件位于同一目录中。
我们首先创建一个名为 Manifest.txt
的文本文件,其中包含以下内容:
Class-Path: MyUtils.jar
警告:文本文件必须以换行符或回车符结束。如果最后一行不以换行符或回车符结尾,则将无法正确解析。
<小时/>然后,我们通过输入以下命令创建一个名为 MyJar.jar
的 JAR 文件:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
这将创建 JAR 文件,其 list 包含以下内容:
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
现在,当您运行 MyJar.jar
时,MyUtils.jar
中的类会加载到类路径中。
关于Java 在没有 IDE 的情况下运行外部 jar 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25072595/