Java Can';t使用Gradle和Intellij导入jar文件依赖项
下面是我的项目目录。我正在尝试使用Kitchen.jar(位于libs文件夹中)作为文件依赖项Java Can';t使用Gradle和Intellij导入jar文件依赖项,java,gradle,dependencies,build.gradle,Java,Gradle,Dependencies,Build.gradle,下面是我的项目目录。我正在尝试使用Kitchen.jar(位于libs文件夹中)作为文件依赖项 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 {
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'
}
下面是我的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'
}
但是,当我运行gradlebuild
时,我得到以下错误,这告诉我Kitchen.jar文件没有正确导入。“食物”是Kitchen.jar中的一个类
在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);
}
}
包装厨房;
/**
*本课程为烹饪食物的烤箱设备建模。
*/
公营烤箱{
私人最终温度;
专用int currentTemperature=0;
/**
*创建默认最高温度为320摄氏度的烤箱对象
*/
公共烤箱(){
这(320);
}
/**
*创建具有特定最高温度的烤箱对象
*
*@param maxTemperature此烤箱可设置的最高温度。不能为负数。
*如果maxTemperature为负值,@将引发IllegalArgumentException
*/
公共烤箱(最高温度){
如果(最大温度<0){
抛出新的IllegalArgumentException(“无效温度”);
}
this.maxTemperature=maxTemperature;
}
/**
*将烤箱的当前温度设置为给定值
*
*@param temperature以摄氏度为单位设置的温度
*@如果温度为负值或高于此烤箱的最高温度,则引发IllegalArgumentException。
*/
公共空隙设定温度(内部温度){
如果(温度<0 | |温度>最大温度){
抛出新的IllegalArgumentException(“无效温度”);
}
此.currentTemperature=温度;
}
/**
*获取当前温度(烤箱没有加热或冷却时间,温度会立即变化)
*@返回当前温度,单位为华氏度
*/
public int getCurrentTemperature(){
返回(当前温度*9/5)+32;
}
/**
*获取此烤箱可设置为的最高温度
*@返回最高温度(以摄氏度为单位)
*/
public int getMaxTemperature(){
返回最大温度;
}
/**
*将食物添加到烤箱中,可能会改变其状态。
*
*@param food要添加到烤箱中的食物
*@param duration食物在烤箱中放置的时间长度(分钟)
*如果food参数为null或持续时间为负数,@将引发IllegalArgumentException
*/
公共空间插入食物(食物,整数持续时间){
if(null==食物){
抛出新的IllegalArgumentException(“食品可能不为空”);
}
如果(持续时间<0){
抛出新的IllegalArgumentException(“持续时间必须大于等于0”);
}
食物。烹饪(当前温度、持续时间);
}
}
类似地,IDEA显示了一个类似的错误,如下所示
我已经尝试通过“项目结构”窗口(见下文)手动添加Kitchen.jar文件作为依赖项,即使根据屏幕截图将其作为依赖项添加,上述错误仍然存在
我还尝试了文件->使缓存无效/重新启动;然而,这并不能解决我的问题
为什么我的Kitchen.jar文件中的类(如“Food”)都没有在我的Gradle项目中注册?
编辑1:
我尝试了0xadecimal的解决方案,如下所示,但不幸的是,它仍然无法编译,Intellij抛出了关于在烤箱类中不解析符号“Food”的相同错误,如下所示
编辑2:
我尝试了卢卡斯·克费尔在评论中的建议,即使用实现文件(…)
;然而,我仍然得到编译错误,Intellij仍然在对我大喊大叫(屏幕截图的右侧)。请注意,每次更改build.gradle文件时,我都会通过点击dependencies
旁边的绿色按钮来确保运行dependencies,即使我已将Intellij设置为在build.gradle
文件更改时自动更新。这意味着问题不应该是因为Intellij中的依赖项没有更新
我认为问题在于,您试图从命名包(
Kitchen.App
)中的类导入未命名包中的类(Kitchen.jar中的任何类)
Java不允许将类从默认(未命名)包导入命名包-这就是导致您看到的编译时错误的原因:
从
类型必须是命名包的成员或
其最外层词汇封闭类型声明(§8.1.3)为
出现命名包的成员或编译时错误
要解决此问题,您可以:
package home.util代码>位于每个文件的顶部
然后,您应该重建jar,并使用import home.util.Kitchen导入App.java
和Oven.java
中的类代码>,导入home.util.Oven代码>,等等
移动代码