1

I'm new to java and I have simple problem. I want to create package with .java file. What I do is File > Project Structure > Dependencies > Add (plus sign on the right side) > JARs and directories... and select directory "MyClasses" which contains "my" directory with "Test.java" file in it. "Test.java" is simply:

package my;

public class Test{
    public Test(){
        System.out.println("Test complete.");
    }
}

Then when I try to use it with import my.Test; it does not work (cannot resolve symbol 'Test'). Even if I try to add this directory again it says it's an "Empty Library" on dependencies tab. What am I doing wrong?

Ava
  • 818
  • 10
  • 18

5 Answers5

3

By the Way you imported is only work for Jar file not for .java.

If you want to import a package containing a java file then you have to copy the pack to the project you working and place it into src/ folder

Now you can import it in your other java file.

Dhanasekaran Don
  • 294
  • 1
  • 14
2

Java source code isn't a project dependency.

It is part of your source tree.

Dependencies are for libraries that come as JAR files.

Source code files go into your src folder, end of story.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

I set up IntelliJ to follow the Maven directory convention

Put your application .java source files under src/main/java. Mark that directory as source root by right clicking on it.

Put your test .java source files under src/test/java. Mark that directory as test root by right clicking on it.

Use the same package structure for both paths.

IntelliJ will build the application and test classes for you.

When you're ready you can add Maven without any changes.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Sounds like you're in File > Project Structure > Modules > Dependencies. I think you need to look in File > Project Structure > Libraries. From there you can add jars which I believe should be referable from your project.

Kosi
  • 263
  • 4
  • 16
0

If you're using Gradle build system, you can easily add a module to the list of source files to be compiled using the settings.gradle file.

Create a file called settings.gradle in your project's root directory(if not present) and then add a path to your module as include '<path>'.

Example: include 'com:example:mymodule'

Sparkzz
  • 367
  • 5
  • 14