Error: The package org.apache.commons is not accessible - java

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)

Related

Why can Netbeans not see a library that is clearly defined and present?

I am working with a NetBeans project that has multiple packages. I will call them package_cam, package_demo, and package_drive. Inside of package_drive I have a class that requires a face recognition SDK that is a JAR and is clearly loaded in my libraries. However, when I run this file, I receive an unsatisfied link error stating that the sdk jar cannot be found. As I stated before, it is listed in my libraries and exists in my lib folder in the project folders. I have searched high and low in Netbeans trying to find a location where that lib must be specified but have come up short. Has anyone ever run into this before? Is there a reason that a certain package would not be able to see or use the library? I am importing the sdk at the head of the file with this
import Luxand.*;
import Luxand.FSDK.*;
import Luxand.FSDKCam.*;
Any help with this would be greatly appreciated!
(I assume you use Netbeans 8 from the question tag, and your project is Ant-based)
In general the recommended way is to use a library wrapper module. There is a wizard so it's very easy, menu File > New Project > NetBeans Modules > Library Wrapper Module. More details in these Netbeans FAQ entries: http://wiki.netbeans.org/DevFaqWhenUseWrapperModule and http://wiki.netbeans.org/DevFaqWrapperModules
If you don't use a wrapper module, you should update project.xml to add the library to the class path, for example:
<class-path-extension>
<runtime-relative-path>ext/hexedit.jar</runtime-relative-path>
<binary-origin>release/modules/ext/hexedit.jar</binary-origin>
</class-path-extension>
See this other FAQ entry: http://wiki.netbeans.org/DevFaqHowPackageLibraries

IntelliJ ctrl-click references a .class file not a .java file

I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all

Netbeans is not recognizing a JAR library

I have a weird problem: Netbeans is not recognizing a JAR file added as a library.
I used to have classes in the default package and I was able to access Thing.jar by calling new Thing(); in the Main class. After I created some packages and reorganized my classes, Netbeans started complaining over that line of code: "cannot find symbol: Class Thing". I tried removing/adding the jar library. It doesn't complain anything and I can see the jar file under projects/libraries. Google also didn't turn up any magic tricks regarding how to add JAR files. I don't understand why this worked inside the default package, but doesn't work in another package.
Have you imported the classes at the beginning of your file.
If a class is not in the same package java requires an import, even when the jar file is placed in the Libraries.
You shouldn't use the default package if you want the class to be available outside the current jar. It is not possible to import from the default package.
See How to import a class from default package

Cannot access class files from another package in Eclipse

I have found similar problems, and i applied all the suggested solutions, but none works so far. I may have overlooked something.I am working on Maven multi-module project. In one of the packages, one of the java files is trying to access class file in another package. Class file is an external dependency provided in a jar file, installed on local repository. Jar file is added in Java Build path-> Libraries under Maven Dependencies. It is even included in classpath variable. But when I try to declare package and access it, I get error
The declared package "net.java.swingfx.waitwithstyle" does not match the expected
package "com.giag.fo.application.utility"
I would really appreciate any suggestions. I have tried anything I could find. I am clueless now.
Here is the image to get better idea
use import instead of the keyword package.
like :-
package com.giag.fo.application.utility;
import net.java.swingfx.waitwithstyle.*;
Instead of
package net.java.swingfx.waitwithstyle;
use
import net.java.swingfx.waitwithstyle.InfiniteProgressAdapter;
You can read more about package and import here

How to import a specific version of a class declared in a package in several locations?

I am trying to import
org.apache.commons.codec.binary.Base64;
However, this class is already being declared in "AWS SDK for Java".
But I have also added the jar file commons-codec-1.8.jar and I want it to load it from there instead. If I use the eclipse interface, showing the possible import options I only get org.apache.commons.codec.binary.Base64 so I don't really know how to force it to load it from the one in my referenced libraries.
Eclipse build a big classpath that contains both jars but the Java runtime will load the
first class it finds.
The AWS SDK uses apache commons codec as a dependency.
If you look at the pom.xml you can see they reference 1.3.
You can either change the pom, or if you don't use maven at all (downloaded the jars from AWS) remove the 1.3 jar from the 3rd party libraries and use only the new one.
Other option is to use 1.3 yourself and drop 1.8 from the classpath.
With standard classloaders it is not possible to load different versions of the same class name.
As this both the jar following same structure it will pick one of them. for that you remove one the .class file which you dont want and import it .

Categories

Resources