Use of Java module java.smartcardio - java

I have trouble to use the module java.smartcardio. In my understanding I just just create a module-info.java and add
module apdu {
requires java.smartcardio;
}
I placed this file directly in the package. But this does not resolve my ``... cannot be resolved to a type`. Also
module com.test.bla.xxx.apdu {
requires java.smartcardio;
}
did not work.
Am I missing a setup step or something? Before now we did not use the module system yet.

Related

Gradle nested dependency issue in Android project

My app has a module (lets call it myModule) which uses a 3rd party library(lets call it 3rdParty). This 3rdParty library is an aar file that uses a video compression library(lets call it videoCompressor). And myModule is also using the same compression library with the same version.
p.s. A module can't use aar file directly, so I had to put it in the app and use it in myModule as mentioned in this post.
So here is the dependency hierarchy
app
dependencies {
implementation(':myModule')
implementation(':3rdParty')
implementation 'videoCompressor:1.2.3'
}
myModule
dependencies {
api project(':3rdParty')
implementation 'videoCompressor:1.2.3'
}
3rdParty is an aar file and it also uses videoCompressor:1.2.3 internally.
While trying to build the project I get this error
Type com.abedelazizshe.lightcompressorlibrary.data.AtomsKt is defined multiple times: /Users/xx/Documents/xx/temp/xx/app/build/intermediates/mixed_scope_dex_archive/devDebug/out/d557fe27570a7fdcab8e1782ef61e44224719c80849bd367030f40a11145859c_1.jar:classes.dex, /Users/xx/Documents/xx/temp/xx/app/build/intermediates/mixed_scope_dex_archive/devDebug/out/23b9eb6594f48cb29b67fd834a2ea73b6ec11312577ac3867a8befdc5815129a_1.jar:classes.dex
This seems to be because there are duplicate classes from the videoCompressor library. How can this be fixed?
I tried removing videoCompressor from the app and myModule, build gets created but there is runtime crash about NoClassDefFoundError.

How to fix error java.lang.module.FindException in Spring (java11)

I have been reading posts in StackOverflow for almost 1 hour and no solutions worked for me.
I am doing a beginner Spring Course using Eclipse and JDK11. And I am having the following error in the terminal:
An error occurred during initialization of boot layer
java.lang.module.FindException: Module proyectoSpringUno not found
this is my java file:
package es.pildoras.IoC;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UsoEmpleados {
public static void main(String[] args) {
ClassPathXmlApplicationContext contexto = new ClassPathXmlApplicationContext("applicationContext.xml");
Empleados Juan = contexto.getBean("miEmpleado",Empleados.class);
System.out.println(Juan.getTareas());
contexto.close();
}
}
I have an error on the import that says:
The type org.springframework.context.support.ClassPathXmlApplicationContext is not accessible
I know that this error happens because I am using the java module system and if I use java 8, all these issues will disappear, but I am not interested in that solution since I want to learn how to use Spring with newer Java JDKs.
At the moment, I have all my libs added into the Classpath but not into the Modulepath.
Solution I tried:
Adding the libs to the modulepath.
Result: this doesn't work. When I add the libs to the modulepath, Eclipse deletes the libs from the classpath ( probably to avoid duplicates). Now I don't have any issues in the module-info (eclipse inserted automodules) or in the import, but when I launch my app, I have the following error:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Stephane\Desktop\curso Spring\ProyectoSpringUno\libs\spring-context-indexer-5.3.6-sources.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.springframework.context.index.processor.CandidateComponentsIndexer not in module
this is the module info:
module ProyectoSpringUno {
exports es.pildoras.IoC;
requires spring.context; // this is automaticly added by eclipse when i add libs to the modulepath
}
Resuming
When I add my dependencies to the modulepath, Eclipse don't work because he wants it in the classpath.
When I add my dependencies to the classpath, eclipse say I need to move some dependencies to the modulepath or the module-info will not work.
And eclipse don't let me add them to both modulepath and classpath because they are duplicates.
Any idea on how to fix this?

hibernate.cfg.xml not found (intellij/maven)

So I am running a hibernate Project in intellij but since I added a module-info hibernate isn't finding the config.
enter image description here
As you can see above the config is in resources where it's supposed to be in a maven project.
Removing the module-info doesnt change anything.
This is my module-info:
module ProjektRechnungsprogramm {
requires java.persistence;
requires java.sql;
requires org.hibernate.orm.core;
requires java.naming;
requires net.bytebuddy;
requires org.hibernate.commons.annotations;}
Even if I try getClass.getResource("hibernate.cfg.xml") it is returning null.
You have placed your config in utils directory so get resource using
getClass.getResource("Utils/hibernate.cfg.xml")

module-info.java compile fail with maven-compiler-plugin and automatic modules

I'm using v3.7.0 of the plugin as required and JDK 9.0.1. I have added two requires statements, each referring to a jar in the class path (automatic module). The module-info.java compiles successfully in Eclipse after I moved the jars to Modulepath. However, Maven gives me a compiler error saying one of them is missing (strangely, not the first one which is just one line before). I tried to check the automatic module name but I get an error from the commands just for this jar. What does this error mean and how do I fix it so that I can discover the proper module name?
I replaced my username in the output below. The jar in question does use a ServiceLoader but is not compiled with Java 9.
computerName:Commander-java username$ jar --file=/Users/username/.m2/repository/com/username/rcf/1.0/rcf-1.0.jar --describe-module
Unable to derive module descriptor for: /Users/username/.m2/repository/com/username/rcf/1.0/rcf-1.0.jar
Provider class com.username.rcf.server.TestCmdChain not in module
computerName:Commander-java username$ java -p /Users/username/.m2/repository/com/username/rcf/1.0/rcf-1.0.jar --list-modules
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for /Users/username/.m2/repository/com/username/rcf/1.0/rcf-1.0.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class com.username.rcf.server.TestCmdChain not in module
The answer in How to deal with java keywords in auto generated module names in Java 9? has a different error related to using a Java identifier in the module name. The automatic jar name for my module should just be rcf since the jar name is rcf-1.0.jar. The error I'm getting is different also.
While deriving module description the contents of any
META-INF/services configuration files are mapped to provides
declarations.
The packages scanned for the services are the ones containing class files.
Also, the package name for individual classes is derived from their fully qualified name. From the shared logs com.username.rcf.server shall be the expected package name for the service to be provided and this shall turn into
provides x.y.z.TestCmdChainInterface with com.username.rcf.server.TestCmdChain
Seems like there is no such package com.username.rcf.server existing in your module.

How to use 3rd party library in Java9 module?

I have some java9 module that uses 3rd party library that is not Java9 module, just a simple utility jar.
However, the compiler complains that it can't find a package from my utility.
What should I do in module-info.java to enable usage of my 3rd party library?
You can use your library as an automatic module. An automatic module is a module that doesn't have a module descriptor (i.e. module-info.class).
But what name do you need to specify to refer to an automatic module? The name of the automatic module is derived from the JAR name (unless this JAR contains an Automatic-Module-Name attribute). The full rule is quite long (see Javadoc for ModuleFinder.of), so for simplicity, you just have to drop the version from its name and then replace all non-alphanumeric characters with dots (.).
For example, if you want to use foo-bar-1.2.3-SNAPSHOT.jar, you need to add the following line to module-info.java:
module <name> {
requires foo.bar;
}
To put it in simple steps, to use a 3rd party jar (e.g. log4j-api-2.9.1.jar below) in your module:-
Execute the descriptor command of jar tool
jar --file=/path/to/your/jar/log4j-api-2.9.1.jar --describe-module
This would provide you an output similar to
No module descriptor found. Derived automatic module.
log4j.api#2.9.1 automatic
In your module descriptor file, declare a requires to that module name as:-
module your.module {
requires log4j.api;
}
That's it.

Categories