![](/img/trans.png)
[英]Can't execute jar file: mainclass attribute is missing Intellij Gradle
[英]Can't import jar file dependency using Gradle and Intellij
下面是我的项目目录。 我正在尝试使用 Kitchen.jar (位于 libs 文件夹中)作为文件依赖项。
下面是我的 build.gradle 文件,我尝试在其中包含 Kitchen.jar 作为依赖项。
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:27.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile files('libs/Kitchen.jar')
}
application {
// Define the main class for the application
mainClassName = 'Kitchen.App'
}
但是,当我运行gradle build
时,我收到以下错误,它告诉我 Kitchen.jar 文件没有正确导入。 “食物”是 Kitchen.jar 中的 class。
这就是烤箱 class 的样子,以了解insertFood
方法的外观。
package Kitchen;
/**
* This class models an oven appliance for cooking food.
*/
public class Oven {
private final int maxTemperature;
private int currentTemperature = 0;
/**
* Creates an Oven object with a default maximum temperature of 320 degrees Celsius
*/
public Oven() {
this(320);
}
/**
* Creates an Oven object with a specific maximum temperature
*
* @param maxTemperature The maximum temperature this oven can be set to. Cannot be a negative number.
* @throws IllegalArgumentException If the maxTemperature is negative
*/
public Oven(int maxTemperature) {
if (maxTemperature < 0) {
throw new IllegalArgumentException("Invalid temperature");
}
this.maxTemperature = maxTemperature;
}
/**
* Sets the current temperature of the oven to the given value
*
* @param temperature The temperature to set in degrees Celsius
* @throws IllegalArgumentException If the temperature is negative or higher than this oven's maximum temperature.
*/
public void setTemperature(int temperature) {
if (temperature < 0 || temperature > maxTemperature) {
throw new IllegalArgumentException("Invalid temperature");
}
this.currentTemperature = temperature;
}
/**
* Gets the current temperature (the oven has no heating or cooling times and changes temperatures instantly)
* @return The current temperature in degrees Fahrenheit
*/
public int getCurrentTemperature() {
return (currentTemperature * 9/5) + 32;
}
/**
* Gets the maximum temperature this oven can be set to
* @return The max temperature in degrees Celsius
*/
public int getMaxTemperature() {
return maxTemperature;
}
/**
* Adds an item of food to the oven, potentially changing its state.
*
* @param food The food to be added to the oven
* @param duration The length of time in minutes the food will be in the oven for
* @throws IllegalArgumentException If the food parameter is null, or if the duration is negative
*/
public void insertFood(Food food, int duration) {
if (null == food) {
throw new IllegalArgumentException("Food may not be null");
}
if (duration < 0) {
throw new IllegalArgumentException("Duration must be >= 0");
}
food.cook(currentTemperature, duration);
}
}
同样,IDEA 显示类似的错误,如下所示。
我尝试通过“项目结构”window(见下文)手动添加 Kitchen.jar 文件作为依赖项,即使根据屏幕截图将其添加为依赖项,上述错误仍然存在。
我也试过 File -> Invalidating Caches / Restart; 但是,这并不能解决我的问题。
为什么我的 Kitchen.jar 文件中的所有类都没有在我的 Gradle 项目中注册?
编辑1:
我尝试了 0xdecimal 的解决方案,如下所示,但不幸的是它仍然无法编译,并且 Intellij 抛出了相同的错误,即未在烤箱 class 中解析符号“食物”,如下所示。
编辑2:
我在评论中尝试了 Lukas Körfer 的建议以使用implementation files(...)
; 但是,我仍然收到编译错误,并且 Intellij 仍在对我大喊大叫(屏幕截图的右侧)。 请注意,每次我更改 build.gradle 文件时,我都会确保通过点击依赖项旁边的绿色按钮来运行dependencies
项,即使我已将 Intellij 设置为在build.gradle
文件更改时自动更新。 这意味着问题不应该是因为 Intellij 中的依赖项没有更新。
I think the issue is that you are trying to import a class from an unnamed package (any of the classes within the Kitchen.jar
) from a class within a named package ( Kitchen.App
).
Java 不允许将默认(未命名)package 中的类导入命名包 - 这就是导致您看到的编译时错误的原因:
该类型必须是命名 package 的成员,或者是其最外层词法封闭类型声明(第 8.1.3 节)是命名 package 成员的类型的成员,否则会发生编译时错误。
要解决此问题,您可以:
确保 Kitchen.jar 中随附的代码属于名为 package。 如果 JAR 仅包含已编译的 class 文件并且您无权访问源代码,那么这不是一个选项。 如果您确实可以访问源代码并且可以重建此 JAR,那么您需要放置一个 package 结构,以便这些类不会全部位于 Z529E625C8C2BF37C6334 AAE495A 的根目录中
主页/实用程序/AbstractFood.java
主页/util/Food.java
主页/util/Fries.java
主页/util/Kitchen.java
主页/util/Oven.java
主页/util/Potato.java
与package home.util;
在每个文件的顶部。
然后,您应该重建 jar 并使用 import home.util.Kitchen 导入App.java
和Oven.java
中的类import home.util.Kitchen;
, import home.util.Oven;
,ETC。
Kitchen
) 中的代码移动到默认 (未命名) package 中。 这不是一个好主意——我们不应该在默认的 package 中编写代码,但如果你不能执行选项 1,这是你唯一的选择。 What this means is moving your java code ( App.java, Oven.java
) to reside directly in the src/main/java
directory, and also removing any package Kitchen
declaration from the top of these files. 您还需要设置mainClassName = 'App'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.