简体   繁体   English

找不到主班

[英]Could not find main class

Okay, so I've been trying to make and executable jar file. 好的,所以我一直在尝试制作可执行的jar文件。 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." 它使用命令“ java -jar bybys.jar”运行,但是当我尝试使用Enter运行它时,出现了一个错误“找不到主类bardejov.Image。程序将退出。”

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. 编译jar文件时,我尝试了与目录的所有可能组合,但不知道问题出在哪里。 I used - C:\\Java\\2D>jar cfm bybys.jar Manifest.txt bardejov/Image.class bardejov/Board.class bardejov/*jpg 我用过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 UPDATE

I fixed it. 我修好了它。 The problem was I didin't have the newest JRE installed. 问题是我没有安装最新的JRE。

There's an extra space character after the name fof the class in your Manifest.txt. Manifest.txt中的类名fof后面有一个多余的空格字符。 This is hinted by the error message: 错误消息提示:

Could not find the main class bardejov.Image .
                                            ^^

The problem actually appears to be that the jar doesn't contain an entry for the directory bardejov ; 实际上,问题似乎在于该jar不包含目录bardejov的条目。 it contains only entries for the files in the directory. 它包含目录中的文件只项。 You can see this in your listing; 您可以在列表中看到它; see how there's an entry for bardejov . 看看bardejov的条目。 When you create the jar file, you have to tell jar to include the directory, not just the files in it: 创建jar文件时,必须告诉jar包含目录,而不仅仅是其中的文件:

jar cfm bybys.jar Manifest.txt bardejov

As from Java 6 you can specify an entry point with the jar command. 从Java 6开始,您可以使用jar命令指定入口点。 The following command should create an executable jar file for your application: 以下命令应为您的应用程序创建一个可执行的jar文件:

jar cfe bybys.jar bardejov.Image bardejov/Image.class bardejov/Board.class bardejov/*jpg

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 : example/Hello.java创建一个Java源文件:

package example;
public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

compile with command 用命令编译

javac example/Hello.java

jar with command 带命令的罐子

jar cfe example.jar example.Hello example/*.class

execute with command 用命令执行

java -jar example.jar

The output is 输出是

Hello world!

Update2 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. 快速修复/工作方法:代替直接启动jar,而是编写一个简短的批处理或shell脚本文件,该文件只需执行java -jar ...命令即可。

open cmd prompt and type 打开cmd提示并输入

set JAVA_HOME=c:\PATH\TO\JAVA_DIRECTORY
set CLASSPATH=.;%JAVA_HOME%\bin;%JAVA_HOME%\lib

make sure to include these variables in your PATH environment variable, as well. 确保也将这些变量包括在PATH环境变量中。

You can get to environment variables in Win7 by going to Control Panel -> System -> Advanced System Settings -> Advanced tab -> Environment Variables. 通过转至控制面板->系统->高级系统设置->高级选项卡->环境变量,可以获取Win7中的环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM