I have a problem moving packages in IntelliJ IDEA. I have created Maven project with multiple modules and each of those modules has a package with the same name. Now whole project becomes a mess if I try to rename some of the packages.
My current project structure is something like this:
--parent-module
|
|--module-one
| |--src
| |--main
| |--java
| |--somepackage
| |--someotherpackages
|--module-two
| |--src
| |--main
| |--java
| |--somepackage
| |--somemorepackages
|--module-three
|--src
|--main
|--java
|--somepackage
|--someevenmorepackages
Notice that somepackage is present in all maven modules.
After MANY created classes in all of those modules and packages I have finally realised what have I done. Now I need to put somepackage into another package under java so I can have something like com.example.moduleone.somepackage.
I have tried renaming package but it renames it in all modules and that creates hailstorm of errors in the code. Also, IntelliJ IDEA renames it to com.example.moduleone.somepackage in all of the modules.
Any help would be highly appreciated.
Might be easiest to create the new package first and then move the classes you want to move into it and then delete somepackage once everything have been moved around
So first add moduleone, then move all of the classes you want to move, then do the same for the other modules, then delete somepackage
Right click to Project name > New> Module..> chose java version and set name for new module.
Drag your packpage your want move to new-module/src > OK > Refactor.
Rebuild project. Find erro : java: packpage dose not exist. Fix it by: Go to erro code > Atl+Enter > Add depedency on module..
Related
Very simple use case, I am using Eclipse Oxygen 4.7.3a that includes support from Java 9. I have two projects that are Java 9 projects:
- projectOne
| - src
| - module-info.java
| - com
| - package1
| - first
| Classificator.java
- projectTwo
| - src
| - module-info.java
| - com
| - package2
| - second
| Classifiable.java
I want to use the Classifiable class inside the Classificator, so I try to import the second module into the first project.
module-info.java Project 1:
module projectOne {
requires projectTwo;
}
module-info.java Project 2:
module projectTwo {
}
Eclipse is giving me the error:
projectTwo cannot be resolved to a module
Do I have to gather all my Java projects under one "main project" in order to let Eclipse know that all those modules are usable inside it? Or have I missed something else?
No, you don't need them to be in the same directory. Ideally, your project-one module defines some uses, which are implements by your project-two module (or vice-versa). Get the runtime implementation of your used interfaces. For this, both jars/classes must be on the module path.
For further information on module build, multi module builds,... you can refer to this link. Even if you do not use gradle, its tutorial on java 9 module build is quite interesting and gives some insight.
While wiring as a service is certainly a viable approach (as described in the other answer), let me add that requires can be made to work, too, of course.
The thing that was probably missing from the first approach is: Eclipse still needs to know where to look for modules. In real world projects this will typically be mediated by your build tool of choice plus a plug-in like m2e or buildship.
If no such plug-in is used, a normal project dependency should be added to the Build Path of project projectOne in order to make project projectTwo visible. Once the project is on the Build Path the module defined by the project can be resolved in references in module-info.java.
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
...
Seems quite logical to get this error. CMake as the other make utilities depend heavily on the order in which the classes get compiled.
From the makefile you've attached it's obvious that the make's entry point is 'xxx.gui.Foo' package problem is though that Foo.java has a dependency to Bar.java which at this point has not been compiled yet.
If you switch the order you could get this to work without any issue, that is Bar.java getting built before Foo.java.
All in all it's a good idea if you ever need to use make files to keep all the utility classes and in general all the classes with as little dependencies to other packages/classes on the top of you make file in order to minimize such problems.
Now, answering as to why you don't get this problem with Maven or javac, in case of the former it's a build tool thus it has a way no resolving all those on it's own. In case of the latter, I suppose that you already had both of the classes compiled hence why it's worked.
I hope the above solves your question.
I have two Java projects, built with ANT, named Project A and B which I created in Luna Eclipse (the Java EE version). The package structure is as follows:
Project A
|
src
|
SomePackage
|
A.java
Project B
|
src
|
AnotherPackage
| |
| B.java
|
SomeOtherPackageInSrc
|
C.java
where A, B, and C are non-abstract POJOs. I also have the following inheritence structure:
C extends B, B extends A.
I added a public method to A, so that its children could have it. I then built project A, and added the resulting JAR to the project B's Build Path. I noticed that C could not access the new method. I then attached the source JAR to the build path, viewed the the source for A.java, and the newly added method was present. I tried a number of things, and adding project A to the Deployment Assembly of project B allowed C to see the new method from A. Why does simply extending the class and adding the jar in which the extended class lives not provide visibility to public methods in this case?
I suppose, only extending and adding the project to your classpath will make it compile, but it is not automatically in the resulting deployment unit (EAR/WAR). ANd this makes the classes unavailable at runtime.
I found the problem. I had multiple versions of the Project A JAR on my classpath, and the compiler picked up the old JAR instead of the new one. Rookie mistake.
So I have two gradle projects: P1 and P2. P2 does not depend on P1 to build, however, during the build phase of P2, I want to build P1, add it to my resources folder and then add that folder to P2's jar.
I am still new to the whole gradle thing, so I am looking for an example build.gradle that would do something like this.
Thanks!
EDIT: It should be noted that P1 and P2 are both modules within the same project.
My project structure looks like this:
Root Project
Root Project
|
|__P1
| |
| |__build.grdale
|
|__P2
| |
| |__build.gradle
|
|__settings.gradle
My settings.gradle looks like this:
include 'P1', 'P2'
So I ended up finding a similar question here: Gradle: how to copy subproject jar to another subproject when task is run?
The build gradle should end up looking something like this:
task fix(dependsOn: ':P1:jar', type: Copy) {
from tasks.getByPath(':P1:jar')
into 'path/to/resources'
}
build.dependsOn fix
Whenever I start a new Java+Gradle project, I create the following directory structure manually:
project-name
|
|--- build.gradle
|
|--- .gitignore
|
|--- src
|--- main
| |---java
|
|--- test
|---java
Since this is a fairly standard structure, I imagine there must be a plugin/command/task for Gradle that would do this scaffolding for me. Is there such a plugin/command/task available? If not, can I write a task for that?
The following does the trick:
gradle init --type java-library
The task is provided by the init plugin
I achieved this kind of scaffolding using an init script. Have a look at my blog post about it.
There is a templates plugin available on github