找不到主类
好的,所以我一直在尝试制作可执行的 jar 文件。它使用命令“java -jar bybys.jar”运行,但是当我尝试使用 Enter 运行它时,它给我一个错误“找不到主类 bardejov.Image。程序将退出。”
这是清单:(
Manifest-Version: 1.0
Created-By: 1.7.0_02 (Oracle Corporation)
Main-Class: bardejov.Image
是的,我使用了新行)
编译 jar 文件时,我尝试了与目录的所有可能组合,我不知道问题出在哪里。我使用 - C:\Java\2D>jar cfm bybys.jar Manifest.txt bardejov/Image.class bardejov/Board.class bardejov/*jpg
目录是:
META-INF/
META-INF/MANIFEST.MF
bardejov/Image.class
bardejov/Board.class
bardejov/siknius.jpg
和主类:
package bardejov;
import javax.swing.JFrame;
public class Image extends JFrame {
public Image() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(477, 530);
setLocationRelativeTo(null);
setTitle("Siknius");
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Image();
}
}
如何使固定?
更新
我修复了它。问题是我没有安装最新的 JRE。
Okay, so I've been trying to make and executable jar file. It runs with the command "java -jar bybys.jar", but when i try to run it with enter it gives me an error "Could not find the main class bardejov.Image. Program will exit."
Here's the manifest :
Manifest-Version: 1.0
Created-By: 1.7.0_02 (Oracle Corporation)
Main-Class: bardejov.Image
(yes i used a new line)
When compiling the jar file I tried every possible combination with the directory, I don't know where the problem is. I used - C:\Java\2D>jar cfm bybys.jar Manifest.txt bardejov/Image.class bardejov/Board.class bardejov/*jpg
The directory is:
META-INF/
META-INF/MANIFEST.MF
bardejov/Image.class
bardejov/Board.class
bardejov/siknius.jpg
And the main class:
package bardejov;
import javax.swing.JFrame;
public class Image extends JFrame {
public Image() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(477, 530);
setLocationRelativeTo(null);
setTitle("Siknius");
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Image();
}
}
How to fix?
UPDATE
I fixed it. The problem was I didin't have the newest JRE installed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Manifest.txt 中类的名称后面有一个额外的空格字符。错误消息暗示了这一点:
There's an extra space character after the name fof the class in your Manifest.txt. This is hinted by the error message:
问题实际上似乎是 jar 不包含目录
bardejov
的条目;它仅包含目录中的文件条目。您可以在列表中看到这一点;看看如何有bardejov
的条目。创建 jar 文件时,必须告诉jar
包含该目录,而不仅仅是其中的文件:The problem actually appears to be that the jar doesn't contain an entry for the directory
bardejov
; it contains only entries for the files in the directory. You can see this in your listing; see how there's an entry forbardejov
. When you create the jar file, you have to telljar
to include the directory, not just the files in it:从 Java 6 开始,您可以使用 jar 命令指定入口点。以下命令应该为您的应用程序创建一个可执行的 jar 文件:
您不需要编写和添加自定义清单。
资源
更新
以下内容在我的机器上有效:
在
example/Hello.java
创建一个 java 源文件:使用命令
jar 使用命令
编译使用命令执行
输出是
Update2
现在看起来比如配置问题。代码和 jar 显然是正确的。
对于快速修复/解决方法:编写一个简短的批处理或 shell 脚本文件,只需执行
java -jar ...
命令,而不是直接启动 jar。As from Java 6 you can specify an entry point with the jar command. The following command should create an executable jar file for your application:
You don't need to write and add a custom manifest.
Resource
Update
The following works on my machine:
create a java source file at
example/Hello.java
:compile with command
jar with command
execute with command
The output is
Update2
Now it looks like a configuration issue. The code and the jar are obviously correct.
For a quick fix/workaroud: Instead starting the jar directly, write a short batch or shell script file that simply executes the
java -jar ...
command.打开 cmd 提示符并键入,
确保将这些变量也包含在您的 PATH 环境变量中。
Win7中的环境变量可以通过控制面板->系统->高级系统设置->高级选项卡->环境变量。
open cmd prompt and type
make sure to include these variables in your PATH environment variable, as well.
You can get to environment variables in Win7 by going to Control Panel -> System -> Advanced System Settings -> Advanced tab -> Environment Variables.