简体   繁体   English

将jar文件中的类导入IntelliJ

[英]Importing a class from a jar file into IntelliJ

I'm having difficulty importing a class and method from a jar file I've created into my .java file in IntelliJ. 我很难将我创建的jar文件中的类和方法导入到IntelliJ中的.java文件中。 It's a simple method to print to the console and contains the following. 这是一种打印到控制台的简单方法,包含以下内容。

public class gazutils {
    public static print(Object text){
        System.out.println(text);
    }
}

Whenever I try to import this with 每当我尝试导入它时

import gazutils.*

I get cannot resolve symbol 'gazutils' 我得到无法解决符号'gazutils'

I am creating the .jar file and importing it into the project structure as follows: 我正在创建.jar文件并将其导入项目结构,如下所示:

Project Structure > Artefacts > Add > Jar > From modules with Dependencies 项目结构>人工制品>添加> Jar>来自具有依赖关系的模块

Then 然后

Build > Build Artefacts 构建>构建人工制品

I then copy the .jar file into the project structure I want to use the .jar file in and 然后我将.jar文件复制到项目结构中,我想在。和。中使用.jar文件

Right Click the jar > Add as Library 右键单击jar> Add as Library

This adds it as a dependency, but I still don't seem to be able to access or import the class or method 这将它添加为依赖项,但我似乎仍然无法访问或导入类或方法

This is because gazutils is a class and not the name of a package 这是因为gazutils是一个class而不是package的名称

and import gazutils.* implies import all that is part of the package gazutils ; import gazutils.*意味着导入所有属于包gazutils一部分; which doesn't exist , and hence the failure! 哪个不存在,因而失败!

A good Idea is to put your class in a package and then import that packageName.yourClassName 一个好主意是将您的类放在一个包中,然后importpackageName.yourClassName

So : 所以:

package somePackage;
//imports and other stuff...
public class gazutils {
public static print(Object text){
    System.out.println(text);
}
}

In your target where you put the Jar : 在你放置Jar的目标中:

import somePackage.gazutils;

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

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