如果添加到包中则无法编译 jar
问题:我编写了一个 hello world java 程序,编译并创建了它的 jar 文件 - 完美,没有问题。
我现在将以下内容添加到源代码的顶部,
package za.ac.uj.csc2a;
保存了代码,将所有文件放在正确的目录中,每次我使用这个批处理文件进行编译时
del *.class
del *.jar
del *.txt
echo compiling HelloWorld.java.....
javac HelloWorld.java
javap -c HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar HelloWorld HelloWorld.class
java -jar za.ac.uj.csc2a.HelloWorld.jar
,我都会得到
Error: Unable to acces jarfile za.ac.uj.csc2a.HelloWorld.jar
任何建议......
这是有效的最终bat文件
del *.class
del *.jar
del *.txt
RD /Q /S bin
echo compiling HelloWorld.java.....
MD bin
javac -d bin za/ac/uj/csc2a/HelloWorld.java
javap -c bin/za/ac/uj/csc2a/HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar za.ac.uj.csc2a.HelloWorld -C bin .
java -jar HelloWorld.jar
pause
非常感
谢艾登
Problem: I wrote a hello world java program, compiled and created a jar file of it - PERFECT, no problems there.
I now added the following to the top of the source code
package za.ac.uj.csc2a;
saved the code, place all the files in the right directories and every time i compile with this batch file
del *.class
del *.jar
del *.txt
echo compiling HelloWorld.java.....
javac HelloWorld.java
javap -c HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar HelloWorld HelloWorld.class
java -jar za.ac.uj.csc2a.HelloWorld.jar
i get,
Error: Unable to acces jarfile za.ac.uj.csc2a.HelloWorld.jar
Any Advice....
Here is the final bat file that works
del *.class
del *.jar
del *.txt
RD /Q /S bin
echo compiling HelloWorld.java.....
MD bin
javac -d bin za/ac/uj/csc2a/HelloWorld.java
javap -c bin/za/ac/uj/csc2a/HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar za.ac.uj.csc2a.HelloWorld -C bin .
java -jar HelloWorld.jar
pause
thanks alot
Aiden
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
-jar
选项接受文件名,而不是类。你应该这样做,
但请记住,你需要在清单文件中指定一个主类,你希望它使用
-jar
选项执行。清单文件应如下所示
,并且您可以将其包含在
jar
命令上的m
选项中。另外,您似乎正在将 .class 文件添加到 jar 文件的根目录中。它应该位于
za.ac.uj.csc2a
目录中如果您不想使用清单文件,您也可以像这样启动它(也许这就是您正在尝试的):
The
-jar
option accepts a filename, not a class.You should just do
But keep in mind that you need to specify a main-class in the manifest file you you want it to execute using the
-jar
option.The manifest file should look like
and you include it with the
m
option on thejar
command.Also, it seems that you're adding the .class-file in the root of the jar file. It should be in the
za.ac.uj.csc2a
directoryIf you don't want to go with a manifest file, you could also launch it like this (perhaps that's what you're trying):
您的 jar 名为 Helloworld.jar:
但您使用包名称作为 jar: 的前缀来调用它
应该是正确的。
通常,如果使用包,则将类和源放在不同的目录中。典型的树看起来像
您可以移动到 src 目录,并使用以下命令进行编译:(
为了方便起见,减少了包/路径长度)如果需要的话,这将在
bin
中生成目录结构(或者通常classes
)然后,要使用正确的目录结构将整个内容打包,请更改为
proj
,并发出:点代表整个目录。
会告诉你更多的选择。要创建可运行的 jar,您还需要一个清单。否则,您可以启动您的应用程序:(
再次缩短路径)。
请注意,目录的结构必须反映包声明。您不能省略一部分 (
za
),并单步进入下一个目录 (ac
)。要运行的类是 za.ac.HelloWorld,因此在 za/ac/ 中搜索它 - 无论是在文件系统中还是在 jar 中。如果您学会从正确的基础发出命令,那就很容易了。如果没有,你将经历多年的尝试和错误。
对于可以使用
java -jar
启动的 jar,您需要一个清单。模板由 jar 命令生成。在那里添加以下空行。再次强调:需要带包的完整名称。
Main-Class
不能写成Mainclass。 HelloWorld 后面不能跟 .class 或 .java - 常见错误!但如您所知,您可以使用 -e 开关来指定主类,但同样:包括包名称。Your jar is named Helloworld.jar:
But you call it with the package name as prefix to the jar:
should be right.
Normally, you put your classes and sources in different directories, if you use packages. A typical tree looks like
You can move to the src-directory, and compile with:
(reduced the package/path length for convenience) This will produce the directory structure in
bin
if needed (or oftenclasses
)Then, to jar the whole thing with the correct directory structure, you change to
proj
, and issue:The dot stands for the whole directory.
will tell you more options. To create a runnable jar you need a manifest too. Else you can start your app:
(shortened the path a bit again).
Note, that the structure of the directory has to reflect the package declaration. You can't omit a part (
za
), and step into the next directory (ac
). The class to run is za.ac.HelloWorld and therefor it is searched for in za/ac/ - no matter whether in the file system or in the jar.If you learn to issue the commands from the right base, it is easy. If not, you'll get years of trial and error.
For a jar which can be startet with
java -jar
, you need a manifest. A template is generated by the jar command. There you addwith a following blank line. Again: the whole name with package is needed.
Main-Class
can't be written as Mainclass. HelloWorld must not be followed by .class or .java - common mistakes! But as you know, you may use the -e switch to specify the main class, but again: Include the package name.您的 jar 文件名为 HelloWorld.jar,而不是 za.ac.uj.csc2a.HelloWorld.jar。很明显,java没有找到它。
jar 文件内容必须遵循包层次结构,因此在 jar 文件内部,
HelloWorld.class
必须位于文件夹za 中/ac/uj/csc2a
。Your jar file is named HelloWorld.jar, not za.ac.uj.csc2a.HelloWorld.jar. So obviously, java doesn't find it.
The jar file contents must respect the package hierarchy, so inside the jar file, the
HelloWorld.class
must be in the folderza/ac/uj/csc2a
.你实际上应该打电话
java -jar HelloWorld.jar
而不是
java -jar za.ac.uj.csc2a.HelloWorld.jar
You should actually call
java -jar HelloWorld.jar
instead of
java -jar za.ac.uj.csc2a.HelloWorld.jar
您确定吗?因为如果您的代码位于包
za.ac.uj.csc2a
中,那么您的源文件必须位于目录za\ac\uj\csc2a
中,您应该编译它使用这样的命令:并且在 JAR 文件中,它应该位于具有该名称的目录中,因此您应该使用这样的命令打包 JAR:
您只能使用
java -jar< 来执行它/code> 如果它是可执行 jar,换句话说,如果您使用正确的
Main-Class
属性将清单文件添加到 JAR 中。如果您没有这样做,您应该能够从 JAR 中运行它:请参阅 在 JAR 文件中打包程序了解如何将应用程序打包到可执行 JAR 文件中以便您可以使用以下命令执行它的详细信息
Are you sure about that? Because if your code is in a package
za.ac.uj.csc2a
then your source file must be in a directoryza\ac\uj\csc2a
, you should compile it with a command like this:and also inside the JAR file it should be inside a directory with that name, so you should package the JAR with a command like this:
You will only be able to execute it with
java -jar
if it is an executable jar, in other words, if you add a manifest file to the JAR with a correctMain-Class
attribute. If you didn't do this, you should be able to run it from the JAR with:See Packaging Programs in JAR Files for details on how to package your app in an executable JAR file so that you can execute it with
您正在创建一个名为 HelloWorld.jar 的 jar:
然后尝试调用不同的 jar 名称(za.ac.uj.csc2a.HelloWorld.jar):
它们需要一致
You are creating a jar called HelloWorld.jar:
Then trying to invoke a different jar name (za.ac.uj.csc2a.HelloWorld.jar):
They need to be consistent