1

I have used Intellj to create a project(Spring, actually, but I found this problem to have nothing between Java project and Spring project) whose hierachy lies below:

  • project_name
    • src
      • main
        • java
          • com.x.y
            • controller_package
              • controller_class.java
            • model_package
              • model_class.java
        • resources
          • THIS.JAR

I don't quite understand, as on the level of java directory, I could create a java class which could directly use the class within THIS.JAR, while the file within those packages, i.e., controller_class.java, I could not find the java class within THIS.JAR. In the former case, when I enter THAT_CLASS in THIS.JAR, the tooltip will pop up with Default package.

This.jar is a jar file created by myself from another project, I used Maven and pom.xml has been configured. This .jar contains some other modules like postgresql/antlr. But on both levels of java directory and controller_package I could import these modules. I have been searched around and found no solution. As a greenhand to Java, it has ruined my day and please, if there is someone who can help?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • It seems that your classes in "this.jar" are in the default package (i.e. don't have a `package x.y;` statement). According to https://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package you can't use classes in the default package from classes within a package. – Thomas Kläger Apr 27 '21 at 08:51
  • When working with Maven, just add your libraries as dependency in your `pom.xml`. Never ever add a JAR to resources. – Gyro Gearless Apr 27 '21 at 08:58
  • @ThomasKläger my desired classes in this.jar exactly have no form like com.xxx.yyy, and the contained classes which could be imported are all in the form of com.xxx.yyy. I suppose this to be the reason...But I am not quite sure how to pack that project into form of x.y in IDEA, as there are ANTLR4 which I need and is utmost hard to change the directory... – C. Augustus Apr 27 '21 at 09:59
  • @ThomasKläger I tried to use reflections, but it seemed not be able to find the class in default package... strange. Anyway, great thanks – C. Augustus Apr 27 '21 at 10:43
  • @GyroGearless thanks for advice. After multiple attempts on configuring pom.xml, I suppose that default package is the reason – C. Augustus Apr 27 '21 at 10:45
  • With ANTLR4 your grammar files should contain a `@Header { package x.y; }` statement and the files should be placed within the package structure like other source files. Then the maven-antlr plugin will create the java files within the correct structure (see https://www.antlr.org/api/maven-plugin/latest/examples/simple.html) - no need for them to be in the unnamed/default package. – Thomas Kläger Apr 27 '21 at 18:01
  • @ThomasKläger Can't thank you more, you literally save my day – C. Augustus Apr 28 '21 at 02:39

1 Answers1

0

if used Eclipse

you can add jar file

1- select right click on your project and select proprieties

2- select java build path

3- select module path and then click on Add External JARS

and then check JRA SYSTEM LIBRARY if the jra file exist or not