Java JPMS: `package xxx is not visible` - java

I have a simple Java project using the non-modularized dependency "io.prometheus:simpleclient_hotspot:0.16.0". It has been working fine until, for other reasons, I wanted to use the Java module system and add a module-info.java to my project. Once I do that, I start getting the compilation error:
error: package io.prometheus.client is not visible import io.prometheus.client.CollectorRegistry;
^ (package io.prometheus.client is declared in the unnamed module, but module simpleserver does not read it)
The prometheus client library isn't modularized, so it is called an "unnamed module". How do I get access to the packages of such a library? I assume I add a dependency in my module-info.java?
This seems like a basic, common JPMS newbie issue, but after doing lots of searches I can't find the solution to this issue.

Related

Are non-exported packages still accessible from the unnamed module?

I am trying to restrict access to a package in my Java application using module-info.java.
For the sake of simplicity, lets say I have the packages org.test.a, org.test.b and this module-info.class:
module test {
exports org.test.a;
}
I then compile that, install the artifact to my local repository and import it in a second project.
There, I am still able to access classes of both packages org.test.a and org.test.b, even though only org.test.a should be accessible.
Only when I modularize that project (by adding a module-info.java that requires the test module), I can no longer compile it:
java: package org.test.b is not visible
(package org.test.b is declared in module test, which does not export it)
Is it not possible to restrict package access to applications that are part of the "unnamed module", or am I just doing something wrong?
I am a bit confused, because even as part of the unnamed module, I am not able to access classes of unexported packages from the jdk (such as jdk.internal.misc.Unsafe).

The package com.sun is not accessible. Java in Eclipse. Classic Problem, but nothing works for me

Im trying to use the HttpServer Class from com.sun package but i can't import it. It keeps saying: "The package com.sun is not accessible."
I've tried every solution i could find in other questions about this topic. I've added a rule to have access to it to my libraries. I changed my JDK to another installed JDK17. I don't know what to do anymore. It's for my college homework, so it would be cool to get it running.
Does someone have a clue?
my code problem:
the access rule:
my current used jdk:
You have a module-info.java so you are using Java modules. Consequently you need to say that you are using the jdk.httpserver module which contains com.sun.net.httpserver.HttpServer.
You do that by adding the line:
requires jdk.httpserver;
to your module-info.java. So something like:
module modulename
{
requires jdk.httpserver;
}
where modulename is the name of your module.
Alternatively delete the module-info.java file to stop using the module system.

Using Jsoup library on eclipse [duplicate]

I have just recently started using Eclipse and am running into problems trying to install external libraries. Following online tutorials, I add the .jar file to the classpath and I see it in the referenced libraries folder. Despite this, when trying to import, I get the error:
The package org.apache.commons is not accessible
For reference, I am trying to install the apache math commons library.
Your code probably has two issues.
First, the import statement is wrong since in Java you cannot add a package itself, but all classes of a package as follows (note .*; at the end):
import org.apache.commons.math4.linear.*;
or a specific class, e.g.
import org.apache.commons.math4.linear.FieldMatrix;
Second, you use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9 and you have Java 12.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

How can I define a Java 9 module inside both a source and source-test directories?

I'm having a really hard time with some Java modules. I'm working on an old Java project which has been "migrated" to Java 11. Nowhere in the project is there any module-info.java file defined.
I'm getting compilation errors in Eclipse/VS Code, which look like:
The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml
I don't fully understand why it's causing the problem, but I added a module-info.java definition to the root of the module.
module com.company.app {
requires java.xml;
}
And that compilation error went away. I now have visibility errors everywhere and many, many more than before.
I've started to fix the visibility errors with exports and imports entries as needed, but now I have a problem.
In one of the projects, there is a source and a separate source-test folder. I've defined a module definition in the source folder.
The code in the source-test folder is separate, but has the same package structure. The following code:
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.experimental.categories.Category;
The import org cannot be resolved. (in the line of the import static).
The type org.junit.Test is not accessible (in the corresponding line)
The type org.junit.experimental.categories.Category is not accessible (once again, in the corresponding line.)
I don't want to add the junit dependency to the main project code, since it's a testing dependency. However, if I define another module-info.java module inside the source-test folder, it complains about the build path containing a duplicate entry 'module-info.java'.
How can the dependencies and modules be correctly defined?

IntellIJ idea does not want to use my external library

I have a school project to build a Jar library containing a basic card management system, then use it in the main project (a blackjack game)
With IDEA I separated the modules, then I built the jar artifact. I now have all my classes in my jar.
The generic card management module name is named Carte (card in french), generated into a Carte.jar file
Now I add this jar as a library for the blakcjack module...
I did an import Carte.*; which give me unresolved symbol Carte ...
Without import, I got unresolved symbol for classes from my library
How am i supposed to use my library?
It looks like the classes in your library are in the default package, i.e. they don't have a specified package name. Carte is the name of the module, but not a Java package. I marked what I mean on this screenshot.
Java doesn't allow importing classes from the default package, as per Java language specification:
It is a compile time error to import a type from the unnamed package.
So, you should avoid the default package to be able to use the classes from a library.
Another possible reason of why the classes seem to be in the default package is that you've improperly packed the jar.

Categories