英文:
How to make IntelliJ IDEA 2022.3.3 recognize my external JAR file?
问题
这是我无法在IntelliJ中运行的代码:
package hjemmestud.grafikk;
public class Grafikk extends EasyGraphics {
public static void main(String[] args) {
launch(args);
}
public void run() {
this.makeWindow("Grafikk", 300, 300);
this.drawCircle(150, 70, 60);
this.drawCircle(150, 190, 60);
}
}
它应该在一个小窗口中创建两个重叠的圆圈,就像这样:程序示例
当前文件结构,IntelliJ项目文件夹:文件结构
可下载的JAR文件网站(第二个文件):https://dbsys.info/programmering/easygraphics/nedlasting.html
我已成功在Windows CMD中使用不同的文件结构运行该程序:使用CMD的文件结构(编译之前) 我通过在CMD中运行以下两行代码来实现的:
javac -classpath .;.\easygraphics.jar Grafikk.java
java -cp .;.\easygraphics.jar Grafikk
然而,当我将文件转移到IntelliJ时,似乎无法工作。我尝试过将JAR文件放在与Grafikk.java类相同的级别,与hjemmestud.grafikk包相同的级别以及与src文件夹相同的级别,既仅仅是JAR文件本身,又是在一个lib文件夹中,但都没有成功。
我当然还添加了JAR文件的路径到外部库,通过修改项目结构中的库和模块,但仍然出现错误,指示无法解析符号'EasyGraphics'。
我还尝试从磁盘重新加载项目并重新构建项目,以及编辑类文件的配置,但都没有成功。
英文:
This is the code I'm unable to run in IntelliJ:
package hjemmestud.grafikk;
public class Grafikk extends EasyGraphics {
public static void main(String[] args) {
launch(args);
}
public void run() {
this.makeWindow("Grafikk", 300, 300);
this.drawCircle(150, 70, 60);
this.drawCircle(150, 190, 60);
}
}
It's supposed to create two circles on top of each other in a small window, like this: Program Example
Current file structure, IntelliJ Project folder:
File Structure
Website of downloadable JAR file (second file): https://dbsys.info/programmering/easygraphics/nedlasting.html
I've successfully run the program using just Windows CMD with a different folder structure: File Structure using CMD (before compiling) I did this by running these two lines of code in CMD:
javac -classpath .;.\easygraphics.jar Grafikk.java
java -cp .;.\easygraphics.jar Grafikk
However, when I transfer the files to IntelliJ, it doesn't seem to work. I've tried having the JAR file on the same level as the Grafikk.java class, on the same level as hjemmestud.grafikk package and in the same level as the src folder, both just the JAR file itself and in a lib folder, but none of it has worked.
I've also of course added the path to the JAR file in the external libraries by modifying the Project Structure, in the Libraries and Modules for each time, but I still get errors up saying it cannot resolve the symbol 'EasyGraphics'.
I've also tried reloading the project from disk and rebuilding the project, aswell as editing the configurations of the class file, without any luck.
答案1
得分: 1
这与用户"CrazyCoder"的评论有关。与JAR文件有关,其中所有类都在默认包中,这使得Java无法导入它们。我通过将我的"lib"文件夹移动到"src"目录,并将"Grafikk.java"类也移动到"src"中来解决了这个问题。然后,在IntelliJ中像通常一样,在项目结构 > 库 中添加JAR文件的路径。在这里了解更多(引用CrazyCoder):https://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package
英文:
It was as user "CrazyCoder" commmented. It has to do with the JAR file where all the classes are in the default package, which makes Java unable to import them. I solved it by moving my "lib" folder to "src" directory, and the "Grafikk.java" class to "src" as well. Then adding the path to the JAR file in IntelliJ as you normally would, in Project Structure > Libraries. Read more about it here (quote CrazyCoder): https://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论