简体   繁体   English

无法使用 Gradle 和 Intellij 导入 jar 文件依赖项

[英]Can't import jar file dependency using Gradle and Intellij

Below is my project directory.下面是我的项目目录。 I am trying use Kitchen.jar (located in libs folder) as a file dependency.我正在尝试使用 Kitchen.jar (位于 libs 文件夹中)作为文件依赖项。

目录

Below is my build.gradle file where I attempt to include Kitchen.jar as a dependency.下面是我的 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'
}

However, when I run gradle build I get the following error, which tells me that the Kitchen.jar file has not been imported correctly.但是,当我运行gradle build时,我收到以下错误,它告诉我 Kitchen.jar 文件没有正确导入。 "Food" is a class in Kitchen.jar. “食物”是 Kitchen.jar 中的 class。

在此处输入图像描述

This is what the Oven class looks like for some context on what insertFood method looks like.这就是烤箱 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);
    }
}

Similarly, IDEA shows a similar error as shown below.同样,IDEA 显示类似的错误,如下所示。

在此处输入图像描述

I've tried manually adding the Kitchen.jar file as a dependency via the "Project Structure" window (see below) and even though it is added as a dependency according to the screenshot, the above errors persist.我尝试通过“项目结构”window(见下文)手动添加 Kitchen.jar 文件作为依赖项,即使根据屏幕截图将其添加为依赖项,上述错误仍然存在。

在此处输入图像描述

I've also tried File -> Invalidating Caches / Restart;我也试过 File -> Invalidating Caches / Restart; however, this doesn't resolve my issue.但是,这并不能解决我的问题。

Why is it that none of the classes in my Kitchen.jar file such as "Food" are registered in my Gradle project?为什么我的 Kitchen.jar 文件中的所有类都没有在我的 Gradle 项目中注册?

Edit 1:编辑1:

I've attempted 0xadecimal's solution as shown below but unfortunately it still fails to compile and Intellij throws the same error about not resolving symbol "Food" in the Oven class as shown below.我尝试了 0xdecimal 的解决方案,如下所示,但不幸的是它仍然无法编译,并且 Intellij 抛出了相同的错误,即未在烤箱 class 中解析符号“食物”,如下所示。

在此处输入图像描述

Edit 2:编辑2:

I've attempted Lukas Körfer's suggestion in the comments to use implementation files(...) ;我在评论中尝试了 Lukas Körfer 的建议以使用implementation files(...) however, I'm still getting the compile error and Intellij is still yelling at me (right hand side of the screenshot).但是,我仍然收到编译错误,并且 Intellij 仍在对我大喊大叫(屏幕截图的右侧)。 Note that every time I change the build.gradle file I make sure I run dependencies by hitting the green button next to dependencies even though I've set up my Intellij to auto update when the build.gradle file changes.请注意,每次我更改 build.gradle 文件时,我都会确保通过点击依赖项旁边的绿色按钮来运行dependencies项,即使我已将 Intellij 设置为在build.gradle文件更改时自动更新。 This means the problem shouldn't be because the dependencies are not updating in Intellij.这意味着问题不应该是因为 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 ). 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 does not allow the importing of classes from the default (unnamed) package into named packages - this is what is causing the compile time error you are seeing: Java 不允许将默认(未命名)package 中的类导入命名包 - 这就是导致您看到的编译时错误的原因:

From the Java Language Specification 7.5.1来自Java 语言规范 7.5.1

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.该类型必须是命名 package 的成员,或者是其最外层词法封闭类型声明(第 8.1.3 节)是命名 package 成员的类型的成员,否则会发生编译时错误。

To fix this issue you can either:要解决此问题,您可以:

  1. Ensure that the code shipped in the Kitchen.jar belongs to a named package.确保 Kitchen.jar 中随附的代码属于名为 package。 If the JAR only contains compiled class files and you don't have access to the source code then this isn't an option.如果 JAR 仅包含已编译的 class 文件并且您无权访问源代码,那么这不是一个选项。 If you do have access to the source code and can rebuild this JAR, then you need to put in place a package structure so that the classes are not all sitting in the root of the JAR, eg:如果您确实可以访问源代码并且可以重建此 JAR,那么您需要放置一个 package 结构,以便这些类不会全部位于 Z529E625C8C2BF37C6334 AAE495A 的根目录中

    home/util/AbstractFood.java主页/实用程序/AbstractFood.java

    home/util/Food.java主页/util/Food.java

    home/util/Fries.java主页/util/Fries.java

    home/util/Kitchen.java主页/util/Kitchen.java

    home/util/Oven.java主页/util/Oven.java

    home/util/Potato.java主页/util/Potato.java

with package home.util;package home.util; at the top of each of the files.在每个文件的顶部。

You should then rebuild the jar and import the classes in your App.java and Oven.java using import home.util.Kitchen;然后,您应该重建 jar 并使用 import home.util.Kitchen 导入App.javaOven.java中的类import home.util.Kitchen; , import home.util.Oven; import home.util.Oven; ,etc. ,ETC。

  1. Move the code in your named package ( Kitchen ) into the default (unnamed) package.将您命名的 package ( Kitchen ) 中的代码移动到默认 (未命名) package 中。 This is not a good idea - we should not be writing code in the default package, but if you can't execute option 1, this is your only option.这不是一个好主意——我们不应该在默认的 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. 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. You will also need to set mainClassName = 'App'您还需要设置mainClassName = 'App'

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

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