My project is started to be large, so I decided to split it into separated modules - gui and domain. My domain module mustn't know that it is a part of Android project, so I created java-library module and put some classes there. It works like a charm but there is a problem when I add some other dependencies inside my java-library module like RxJava. I've created a Main class to simply test my code. Builds passes, but when it tries to invoke RxJava classes it crashes with exception:
Exception in thread "main" java.lang.NoClassDefFoundError: io/reactivex/Observable
at com.example.domain.Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: io.reactivex.Observable
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
My Main class:
package com.example.domain;
import io.reactivex.Observable;
public class Main {
public static void main(String[] args) {
Observable.just(1)
.subscribe(integer -> System.out.println(integer));
}
}
My build.gradle file:
apply plugin: 'java-library'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api 'io.reactivex.rxjava2:rxjava:2.1.2'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
1) If I use com.android.library instead it works
2) If I add RxJava library as a .jar file it changes nothing
EDIT:
I looked into Domain.iml file and I found something strange:
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Domain" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":Domain" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="true" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/build/classes/java/main" />
<output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
</component>
</module>
The weird part is scope attribute in orderEntry node
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
If I remove it manually:
<orderEntry type="library" exported="" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" />
It works like a charm.
Unfortunately if I sync my build.gradle file it overrides my modification.
How to make it works permanently and why this acts like this?
The weird part is scope attribute in orderEntry node
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
If I remove it manually:
<orderEntry type="library" exported="" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" />
It works like a charm.
Unfortunately if I sync my build.gradle file it overrides my
modification.
Have you tried to change the scope via Android Studios Project Structure?
File -> Project Structure then select the Dependencies Tab and add the Dependency for the required module. In your case that would be java-library I believe.
As far as i know you can't change the scope of the dependency once added. So if you have already added it, try to remove and add it again. Be sure to assign the implementation scope though and see if it works.
I hope this was helpful
Related
Edit - this is solved - the issue was solved by setting: "Settings -> Build, Execution, Deployment - Java Compiler -> Project Bytecode Version : 9"
I'm following the example/tutorial at https://www.logicbig.com/tutorials/core-java-tutorial/modules/getting-started-in-intellij.html
This is NOT a maven project. I have set the source level to 9 in the project structure eg
I have set the project level to 9:
I am getting "Error:(3, 4) java: modules are not supported in -source 8
(use -source 9 or higher to enable modules)" error when trying to build/run the project's main class.
And yes I have seen Intellij IDEA 2018.1 Jdk10 cannot run test which is unanswered.
I have just updated Intellij to 2019.2.4 but the issue remains. I am using SDK 11.
How can this be fixed? Thanks.
Edit: the Run/Debug configuration options:
Edit: the error:
Edit: the platform SDK:
Edit: the dependencies panel:
FWIW, the contents of the iml files show language level 9:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="common.widgets" />
</component>
</module>
This is solved - the issue was solved by setting: "Settings -> Build, Execution, Deployment - Java Compiler -> Project Bytecode Version : 9"
Thanks to all who gave their time to try to help.
I think you must be setting SDK under Platform Setting in project structure
I'm trying to integrate my fxml file to my project, using the following code,
final FXMLLoader fxmlLoader =
new FXMLLoader(this.getClass().getResource("/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
The program crashes at the second line, throwing this exception,
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/1199823423.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at FileActions.start(FileActions.java:42)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/970544503.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1878510174.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/1792840102.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1671111378.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Process finished with exit code 130
I also tried to used the exact path (using copy path), but I still got the same error.
Also tried,
Parent root = FXMLLoader.load(getClass().getResource("../resources/sample.fxml"));
//and
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
and also put it in the same folder of my java files, but none works...
I don't know if this is relevant, but here is my iml file,
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" exported="" name="commons-net-3.6" level="project" />
<orderEntry type="library" exported="" name="hamcrest-core-1.3" level="project" />
<orderEntry type="library" exported="" name="junit-4.12" level="project" />
</component>
</module>
What causes this and how could I fix it?
Here is a zip of my project if anyone may want to take a look.
Thanks,
Henry
The error seems to indicate that the fxml file is being loaded from a wrong location. getClass().getResource loads a resource which per Java Spec for a Resource is identified by a string consisting of sequence of substrings, delimited by (/) followed by resource name. Each substring must ne a valid Java identifier. There is no true guarantee of resource resolution with .. as it's not a valid a identifier.
Am assuming you are not using Maven folder structure, so I would suggest
simply keeping the fxml file in the same package as that of Java class that launches it and set the path like "sample.fxml"
What do you see when you output getClass().getClassLoader().getResource("sample.fxml")
Also make sure your filename matches with the resource name, no spaces before or after - you can rename and check on the spaces, if any.
I see you are using maven and jetbrains. This exception does indeed come when the fxml file's location is wrong in the code. You are using maven. Maven is searching for resources (like fxml files) in it's resources folder. The maven project's root folder is where pom.xml is. I will reference it as {mavenRoot}.
So let's assume, your fxml file is in this path:
{mavenRoot}/src/main/resources/fxml/sample.fxml
Then you can use theese two lines to set up your fxml loader:
FXMLLoader loader1 = new FXMLLoader();
loader1.setLocation(getClass().getResource("/fxml/sample.fxml"));
In a maven project, the getResource() will search the {mavenRoot}/src/main/resources folder.
I have a relatively simple 3-module IntelliJ project built using Gradle. I'm new to both Gradle and IntelliJ, and am working on getting the tooling/process understood between two of my personal machines.
The projects are stored in Git, but every time I open IntelliJ on one of my respective machines (after pushing changes from the other) my module files are always changed:
modified: .idea/modules/core/core.iml
modified: .idea/modules/service/service.iml
modified: .idea/modules/web/web.iml
The changes are always just the reordering of my library entries:
- <orderEntry type="library" scope="TEST" name="Gradle: io.netty:netty-resolver:4.1.6.Final" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: io.projectreactor:reactor-core:2.0.8.RELEASE" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: org.yaml:snakeyaml:1.15" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: org.jodd:jodd-core:3.7.1" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-all:1.1" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: org.objenesis:objenesis:1.2" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: cglib:cglib-nodep:2.2" level="project" />
- <orderEntry type="library" scope="TEST" name="Gradle: org.reactivestreams:reactive-streams:1.0.0" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: com.fasterxml.jackson.core:jackson-annotations:2.7.0" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: org.reactivestreams:reactive-streams:1.0.0" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: cglib:cglib-nodep:2.2" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: org.objenesis:objenesis:1.2" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-all:1.1" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: org.jodd:jodd-core:3.7.1" level="project" />
+ <orderEntry type="library" scope="TEST" name="Gradle: org.yaml:snakeyaml:1.15" level="project" />
And the changes are significant:
$ git diff --stat
java/.idea/modules/core/core.iml | 204 ++++++++++----------
java/.idea/modules/service/service.iml | 276 +++++++++++++--------------
java/.idea/modules/web/web.iml | 338 ++++++++++++++++-----------------
This happens every time I swap from one machine to the other.
Is there any way to convince IntelliJ to keep these entries in the same order? It's getting extremely annoying for each one of my commits to have these 3 files included along with whatever other changes I'm making.
I obviously can't .gitignore these three files, as they contain all the other information about my module's configuration - something that it completely makes sense to share.
Note that I'm not using apply plugin 'idea' in my build files, I'm just letting IDEA manage the modules entirely. Is this my mistake?
I obviously can't .gitignore these three files, as they contain all
the other information about my module's configuration - something
that it completely makes sense to share.
Actually, you can. These files will be generated from Gradle when you open the project and Synchronize it with Gradle.
See the FAQ about sharing project files and the comments below.
Related bugs:
IDEA-165941 Gradle: orderEntry's in .iml are sorted differently on Refresh with and without change in build.gradle
IDEA-157363 Android: arbitrarily reordering of orderEntry tags in iml files w/o related changes in gradle configuration
I am using IntelliJ Idea 14.1.4 and Gradle, and I am trying to create sub projects and nested sub projects:
Project
|--Research
|--Development
|--Algorithms
The reason for this is that research algorithms are separate from the development implementation. They are written in different languages (Python vs. Java) and used by different programmers, however I still want to maintain one Gradle project, so eventually researchers would be able to use stable existing algorithms implementations when developing new ones.
My problem is that Idea doesn't recognize properly the Gradle structure of the second level Gradle sub project: Project is recognized well, Research and Development are recognized well, but the Algorithms sub project is not recognized:
The code inspection of the gradle.build file in Algorithms project is highlighted as a warning with a message that test cannot be applies to Groovy.lang.closure Note that below is the complete build.gradle:
test{
useTestNG()
testLogging.showStandardStreams = true
}
Also, when I try in Idea Run->Edit Configurations->Defaults->Gradle and trying to choose Gradle project, I see that in the pop-up menu Development and Research do appear as sub-projects of Project, but Algorithms appears as a stand alone project.
Making the project however does create all the binaries and manually running a test case also works well.
When I run Gradle for command line, it appears to be working well and recognizing all nested projects: All the code is being compiled and tests are being ran. So the question here is if there is an Idea issue here which is not working correctly with Gradle, or am I doing something wrong?
Code for build.gradle inside Project:
allprojects {
group 'com.project'
version 'v0.1'
repositories {
mavenCentral()
}
}
Code for settings.gradle inside Project
rootProject.name = 'Project'
include 'Development'
include 'Development:Algorithms'
include 'Research'
Code for build.gradle of Development sub-project:
subprojects {
apply plugin: 'java'
repositories {
jcenter()
}
dependencies{
testCompile 'org.testng:testng:6.9.4'
}
}
Other code samples that I might be writing wrong (most of them are auto generated by Idea:
Project.iml:
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Project" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build" />
<output-test url="file://$MODULE_DIR$/build" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Development.iml
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Development" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build" />
<output-test url="file://$MODULE_DIR$/build" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Algorithms.iml
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Development:Algorithms" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="com.project" external.system.module.version="v0.1" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/plugins/testng/lib/testng.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
I would like to point the following:
All projects and sub-projects have the same group: com.project
In Algorithms.iml the original (auto generated) value of external.linked.project.id was :Algorithms. I have tried setting it to :Development:Algorithms to see if it will change anything. After each change I have ran rebuild.
In Algorithms.iml the original (auto generated) value of external.root.project.path was $MODULE_DIR$/... I have tried setting it to $MODULE_DIR$/../.. to see if it will change anything. After each change I have ran rebuild.
****************************Edit********************************
Further investigation shows an issue with the workspace.xml file:
There is a GradleLocalSettingsElement there that was automatically created, but appears to be wrong: There are two different entries: one for Development and Devlopment:Algorithms and onw for the rest, so I might have did something wrong when I created the project.
My question is if there is a reference for the structure of this file and specifically for the elements inserted by the Gradle plugin?
<map>
<entry>
<key>
<ExternalProjectPojo>
<option name="name" value="Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
</key>
<value>
<list>
<ExternalProjectPojo>
<option name="name" value="Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Development:Algorithms" />
<option name="path" value="$PROJECT_DIR$/Development/Algorithms" />
</ExternalProjectPojo>
</list>
</value>
</entry>
<entry>
<key>
<ExternalProjectPojo>
<option name="name" value="Project" />
<option name="path" value="$PROJECT_DIR$" />
</ExternalProjectPojo>
</key>
<value>
<list>
<ExternalProjectPojo>
<option name="name" value="Project" />
<option name="path" value="$PROJECT_DIR$" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Development" />
<option name="path" value="$PROJECT_DIR$/Development" />
</ExternalProjectPojo>
<ExternalProjectPojo>
<option name="name" value=":Research" />
<option name="path" value="$PROJECT_DIR$/Research" />
</ExternalProjectPojo>
</list>
</value>
</entry>
</map>
Here is my code
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static sun.nio.cs.Surrogate.is;
public class PlayerTest {
public void should_return_3_when_status_is_3(){
Player player = new Player();
assertThat(player.getStatus(),is(3));
}
}
And here is the trace
Can't find symbol
符号: method assertThat(int,boolean)
位置: class PlayerTest
my iml file is
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.10.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
I think I used it before,but it not works now.
Have no clue about what to do.However assertTrue is working.
Ubuntu 11.04 is being used.
The mistake is in
import static sun.nio.cs.Surrogate.is;
is() should return a matcher, but whatever this is, it returns boolean. Try org.hamcrest.Matchers.is or org.hamcrest.CoreMatchers.is instead.