IntellIJ idea does not want to use my external library - java

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.

Related

Java JPMS: `package xxx is not visible`

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.

How to get Eclipse to stop asking to create a module-info java file on new Java project creation?

Everytime I try to create a new java project Eclipse keeps asking if I want to add a module-info java file to the source folder. It's getting pretty annoying as there's no immediately obvious option to opt out of this check.
IDE for Java Developers, Photon release 4.8.0
See while creating a new project, after you click>> next on the very first dialog "new java project." There is one another dialog box pops up when you click >> finish. It will lead you to the 3rd dialog box which asks for the creation of module-info java file?? & gives you two option create & don't create.
You should select "don't create."
Here are some advantages of the file
module-info.java contents:
To declare a jar file as a named module, one needs to provide a module-info.class file, which is, naturally, compiled from a module-info.java file. It declares the dependencies within the module system and allows the compiler and the runtime to police the boundaries/access violations between the modules in your application. Let’s look at the file syntax and the keywords you can use.
Module module.name – declares a module called module.name.
Requires module.name – specifies that our module depends on the module module.name, allows this module to access public types exported in the target module.
Requires transitive module.name – any modules that depend on this module automatically depend on module.name.
Exports pkg.name says that our module exports public members in package pkg.name for every module requiring this one.
Exports pkg.name to module.name the same as above, but limits which modules can use the public members from the package pkg.name.
Uses class.name makes the current module a consumer for service class.name.
Provides class.name with class.name.impl registers class.name.impl class a service that provides an implementation of the class.name service.
opens pkg.name allows other modules to use reflection to access the private members of package pkg.name.
Opens pkg.name to module.name does the same, but limits which modules can have reflection access to the private members in the pkg.name.
One great thing about the module-info.java syntax is that the modern IDEs would fully support your efforts of writing them. Perhaps all of them would work beautifully. I know that IntelliJ IDEA does content assist, quick fixes of the module files when you import classes from the module you haven’t required yet, and so on. I don’t doubt Eclipse IDE and NetBeans IDE offer the same.
Perhaps this is not a perfect solution, but it will stop asking if you choose to use Java version 8 compiler (JavaSE-1.8). If you need any newer Java version, I'm affraid don't have an answer.

java jigsaw extend jar library

I am trying to separate a project of mine into Jigsaw Modules. One pain point has been to use .jar libraries in multiple modules and extend their functionality in some of them.
To be more concrete, I am using minimal-json-0.9.1.jar via "requires transitive minimal.json" in a module-info.java file and I set the module to be on the modulepath in eclipse.
I then created this interface:
import com.eclipsesource.json.JsonObject;
public interface JsonSerializable {
JsonObject toJson();
}
Note that JsonObject comes from the minimal.json library.
Now I get a warning message in eclipse: "The type JsonObject is not exported from this module". How can I get rid of it?
Suggestion: Do not transitively rely on an automatic-module and change your module-descriptor to make use of
requires minimal.json;
Explanation: While including the transitive modifier, an implied readability is expressed by the requires clause. This means that any module that depends upon your module [requires your.module] will also be able to read the minimal.json module.
The warning from eclipse seems to be highlighting the same point that the use of JsonObject in any further module that requires your module would not be because your module have explicitly exported [exports some.package;] the package including the class, rather because of the transitive dependency.

Calling static methods from a class defined in another project requires libraries added for both projects

I have two projects in my NetBeans window
MyProject
Tester.java
Utilities
Utils.java
The Utils.java file contains a number of static methods written by others that we can re-use. Recently I added a new set of static methods to Utils.java that uses new external jar's. I added the libraries to the Utilities project via Properties --> Libraries --> Add Library
I then proceeded to call these methods from within Tester.java but received java.lang.NoClassDefFoundError exceptions for the classes that were defined in those external libraries. This does not occur when I call the methods from within the Utilities project.
I solved the problem by adding the required libraries to the MyProject project as well, but is there a reason why I have to do this?
You need to have the external libraries in your MyProject as well because it is transitively dependent on those libraries. You are getting java.lang.NoClassDefFoundError because the required classes were available for the Utilities during the compile time to build the jar but those classes are missing at the runtime.
The reason is simple: Utils.class relies on the classes from the library to work. So if you don't have the classes of the library in the classpath, Utils.class can't work. Just like just having an accelerator is not sufficient to make a car move. Without the car engine, the accelerator can't work. The fact that you, as a driver, don't mess with the engine directly, but only through the accelerator, doesn't mean the engine is not necessary. (sorry for this car analogy, but hopefully it makes things clearer).

accessing referenced libraries from packages in eclipse

I can only access referenced library classes if I save my classes in the default package. If I try to access them from any other package I get "className cannot be resolved". Any idea why this could happen?
That package is from the standard library of Princeton's IntroCS Course after a quick Google.
If you follow down to the FAQ on the page
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no
longer access the libraries in stdlib.jar. Why not?
A. The libraries
in stdlib.jar are in the "default" package. In Java, you can't access
classes in the default package from a named package. If you need to
use our libraries with a named package, you can use the packaged
version stdlib-package.jar.
Download package jar file:
http://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar
Right click project folder and add external JAR.
import edu.princeton.cs.introcs.*;
Add above line to the classes where you need to reference the classes.
The first line references the correct package name and the * wildcard imports all the classes within it.
:) Hope that helps.
//Edit - If you right click the project folder and use "Organize Imports" it will be faster so you don't have to manually add to each class.
Check if you've proper import declarations and the type you are refering to has public access modifier.
Import declaration for Types in the default package are per definition impossible. JLS specifies that it is a compile time error to import a type from an unnamed package.
You must access your class via reflections or much better do not ever use the default package. Eclipse should show you a warning when you want to create a type inside default package, because it's generally discouraged.
If you're using an IDE like Eclipse then try hitting CTRL+SPACE behind the type name in the class where you want to use it. Eclipse should give you all matching opportunities and will add the import automatically for you if you select your class.
You need to import the package into other package.
Classes in one package cannot directly reference to other package unless its get referenced.

Categories