"package xxx.util does not exist" error while using cmake in a java project

时间:2018-03-25 20:58:53

标签: java makefile cmake packages manifest

I have to write a java project for a course and our professor requires us to use CMake as a build tool. In my Entry Point (main class) I want to use a class located in another package. While trying to compile it I get the following error message (attached below).

Using this wildcard import in Foo.java

import xxx.util.*;

helped, but I'm sure that this isn't the most elegant to achieve my goal.

I'm not able to figure out how to solve this problem as I get no problems using another built tool (maven) or manually compiling it. But as I must use CMake I hope that anyone out there had similar problems using this kind of project setup and is able to help me.

I listed all the relevant project information (structure, affected classes, CMakeLists.txt, ...) below.

Feel free to ask questions in the comments section in order to clear off any ambiguities.

Thank You!

Relevant Project structure:

.
|-- CMakeLists.txt
|-- build
|-- resources
|   |-- MANIFEST.MF
|   \-- sqlite.jar
|-- src
|   \-- xxx
|       |-- gui
|       |   \-- Foo.java
|       \-- util
|           |-- Bar.java

Foo.java:

package xxx.gui;

import xxx.util.Bar;

public class Foo {

    public static void main(String[] args) {
        Bar bar = Bar.getBar(); //singleton 

    }
}

Bar.java:

package xxx.util;

public class Bar {

}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5) 
project(xxx LANGUAGES Java)
find_package(Java 1.8 REQUIRED COMPONENTS Development)
include(UseJava)
file(GLOB_RECURSE SOURCES "src/*.java") 
file(GLOB_RECURSE JARS "resources/sqlite*.jar")
add_jar(DataFX ${SOURCES} MANIFEST ./resources/MANIFEST.MF INCLUDE_JARS ${JARS}
ENTRY_POINT xxx.gui.Foo)

MANIFEST.MF:

Class-Path: ../resources/sqlite.jar

Relevant Error message:

gui/Foo.java error: package xxx.util does not exist
import xxx.util.Bar;
                  ^
gui/Foo.java error: cannot find symbol
        Bar bar = Bar
        ^
  symbol:   class Bar
  location: class Foo
...

1 个答案:

答案 0 :(得分:1)

似乎很合理地得到这个错误。 CMake作为另一个使得实用程序在很大程度上依赖于编译类的顺序。

从你附加的makefile中可以明显看出make的入口点是'xxx.gui.Foo'包问题,尽管Foo.java依赖于Bar.java,此时尚未编译。

如果你切换顺序,你可以毫无问题地使用它,那就是在Foo.java之前构建Bar.java。

总而言之,如果您需要使用make文件来保存所有实用程序类,并且通常所有类都依赖于其他程序包/类的依赖项,以便最小化这一点,这是一个好主意。这样的问题。

现在,回答为什么你没有用Maven或javac来解决这个问题,如果是前者它是一个构建工具,那么它就有办法解决它自己的所有问题。如果是后者,我想你已经编译了两个类,因此它的工作原理。

我希望以上解决你的问题。

相关问题