Java compile error: package does not exist

2019-05-06 00:29发布

问题:

In my workspace (wsPrivate) I have 3 gradle projects.

  • Hangman
  • PixelView
  • Reports

Projects PixelView and Reports compile fine. However Hangman uses those both projects and some how can't find the packages while compiling.
See the error below (which pop up during compiling Hangman):

D:\_wsGitlab\wsPrivate\Hangman\src\main\java\game\core\Game.java:10: error: package hangman does not exist
import hangman.Messages;
              ^
D:\_wsGitlab\wsPrivate\Hangman\src\main\java\game\core\Game.java:13: error: package viewer does not exist
import viewer.Box;
             ^
D:\_wsGitlab\wsPrivate\Hangman\src\main\java\game\core\Game.java:14: error: package viewer.figures.boxes does not exist
import viewer.figures.boxes.Borders;

I've read almost every bit of info I could find about classpaths and how to fix this [in my opinion] "scope-problem" as also I've tried multiple solutions but I think I'm making it worse. The program does work fine without compiling in eclipse, so I assume the code withing is correct.

I'm now like 3 days stuck on this so if anyone could explain to me where I'm driving of the road that would be really nice. If anymore information is wanted: just ask ;)

So this is basically what I have at the moment:

Layout of my packages and added library:

Custom library that I've added to Hangman:

Projects on build path of project Hangman:

Order and export of project Hangman:

Other two projects that are referenced (pixelview / reports):

Last but not least my build.gradle:

apply plugin: 'application'

mainClassName = "game.core.Game"

repositories {
    jcenter()
}

dependencies {
    compile 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.12'
}

jar {
    baseName = 'HangmanGame'
    version = '2.00.0'
    manifest {
        attributes(
        'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
        'Main-Class': 'game.core.Game'
        )
    }
}

回答1:

You say you have three projects - I interprete that as one Gradle project with three separate Gradle subprojects.

If so, you need a settings.gradle file at the root level with contents like

include 'Hangman', 'PixelView', 'Reports'

You can read more about authoring multiproject Gradle builds in the Gradle documentation.



回答2:

I have met the same problem under docker uBuntu (Without Android Studio Installed), with all the settings set as the indicated above, still got same error.

Then I found there was no local.properties file under project root, after copied local.properties from another project and correctly set up the ndk and sdk path, multi projects built successfully, my ndk/sdk path maybe different from yours.

Also this project can be built successfully on other machine with android studio installed even missing following files: local.properties(will appear after first build), gradlew, gradlew.bat, gradle-wrapper.jar

ndk.dir=/home/username/Android/Sdk/ndk-bundle
sdk.dir=/home/username/Android/Sdk