IntelliJ IDEA创建的JAR文件无法运行 [英] JAR files created by IntelliJ IDEA do not run

查看:262
本文介绍了IntelliJ IDEA创建的JAR文件无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序跨越IntelliJ中的几个类,当我在IDE中对其进行测试时,它可以正常工作.但是,每当我按照教程将项目制作为.jar可执行文件时,它都不会运行.

I wrote a program that spans a few classes in IntelliJ and it works fine when I was testing it in the IDE. However, whenever I follow tutorials to make my project into a .jar executable, it does not run.

双击out文件夹中的文件不会运行,并说"Java JAR文件"projectName.jar"无法启动.

The file in the out folder does not run when double-click on it and says "The Java JAR file "projectName.jar" could not be launched.

从终端打开它时,出现错误找不到或加载主类".我使用了"$ java -jar projectName.jar"

When opening it from the terminal, I get the error "Could not find or load main class". I used "$ java -jar projectName.jar "

我遵循了以下步骤:

  1. 打开项目结构,工件,然后单击(+).
  2. 在JAR下,来自具有依赖项的模块.
  3. 使用我的主要方法指向该类,然后单击确定".
  4. 单击应用",然后单击确定"退出.
  5. 然后我关闭项目结构并打开Build,然后构建Artifacts ...
  6. 然后单击构建".
  7. 导航至〜/IdeaProjects/projectName/out/artifacts/projectName_jar/projectName.jar

然后我将尝试运行它,但它不会运行.我的项目既有GUI,又可以从命令行运行.当我在Terminal中编译main方法时,它可以正常工作,但是我需要一个JAR文件.我不确定应该在问题中包含什么内容,因为我确定我的代码无关.我也利用Maven,不确定是否相关.在构建JAR之前,我还应该做其他事情还是要添加到项目中吗?

Then I would try to run it but it does not run. My project has both a GUI and can be run from command lines. When I compiled the main method in Terminal, it worked fine, but I need a JAR file. I'm not sure what I should include in the question, since I'm sure that my code is unrelated. I also utilize Maven, not sure if that is related. Are there other things that I should be doing or adding to my project before I build the JAR?

推荐答案

这可能是由于在错误的位置创建清单所导致的.

This can be a result of the Manifest being created in the wrong place.

首先确保它在src/main/resources/META-INF/MANIFEST.MF

(Intellij有时会错误地将其放置在/src/main/java/META-INF/MANIFEST.MF中)

(Intellij sometimes incorrectly places it in /src/main/java/META-INF/MANIFEST.MF)

然后确保该jar在/java或/resources中的 src 目录中寻找META-INF.这意味着:

Then Make sure the jar is looking for META-INF in the src dir NOT in /java OR /resources. This means:

设置文件> 项目结构> 工件> + > JAR kbd >> 从具有依赖性的模块... 中,您的META-INF/MANIFEST.MF目录应该以src结尾.示例:

When you set up File>Project Structure>Artifacts>+>JAR>From modules with dependencies... then your Directory for META-INF/MANIFEST.MF should end at src. Example:

ONLY C:\Users\User\StudioProjects\Project\src\
NOT THE PREPOPULATED C:\Users\User\StudioProjects\Project\src\main\java

如果您的项目结构 jar已经设置好,请将 Manifest File 路径更改为:

If your Project Structure jar is already set up change the Manifest File path to:

C:\Users\User\StudioProjects\Project\src\META-INF\MANIFEST.MF
INSTEAD OF C:\Users\User\StudioProjects\Project\src\main\java\META-INF\MANIFEST.MF

这篇关于IntelliJ IDEA创建的JAR文件无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆