Cannot access class files from another package in Eclipse - java

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

Related

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

How to import source from GitHub into an Eclipse project

I am trying to use this tool from Google: https://github.com/pcj/google-options
In the example given by Google, they use:
import com.google.devtools.common.options.OptionsParser;
to allow their example code to use the command line parser.
When I do this, Eclipse says the referenced files don't exist.
I have tried putting the source file from google-options in a package in my project; no luck. I tried running Google's example, and it won't even compile. It can't find all of the classes in its own src folder.
Based on my research, I think there's either an issue with a .jar file needing to be in the build path, something to do with Apache Ant, or an issue with GitHub I don't understand.
I'm very new to GitHub; this is the first time I've tried to do anything like this.
You should first import this project from GitHub to your local machine. import statement in your class means that classes from other packages should be imported to this class you're working on. Import not from the remote source, but from local.
I figured it out. It needs the guava.jar file found here added to the buildpath. After that, it has a couple weird dependency issues with javax, but I just removed those (it was Nullable and Immutable, if anyone's interested).
First you must clone the project and compile it, to do that put the following commands:
Open your terminal and go to the workspace folder:cd {workspace}
Clone the project: git clone https://github.com/pcj/google-options.git
Compile the project: mvn clean install
Check the result, this should print: BUILD SUCCESS
In that step, maven will download all dependencies that the project needs.
To import the project: Launch eclipse, click on File /Import... choose Existing Maven project, browse your workspace where the projetc was cloned.
Create a class that extends OptionsBase and defines your #Option(s). For more information about how to use, see https://github.com/pcj/google-options#usage

How to find and add libraries from import statements?

I'm working with a code snippet that came with a bunch of import statements, which I copied as well. The problem is that the libraries apparently aren't packaged along with Intellij, so the usual Alt+Enter doesn't help. The IDE shows a "cannot resolve symbol" at almost all of the libraries. Now, I solved one of them by grabbing the correct .jar on the internet somewhere and adding it to the project structure, but I'm having a much harder time locating another one, plus this seems to me like an awfully inefficient way to work.
How do I locate, download, and add the required classes/libraries to the project in an efficient, clean way, based on import statements only?
Imports just for reference:
import org.apache.log4j.Logger;
import org.junit.Test;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
You need to find out in which jar your class is included. I often use http://www.findjar.com/index.x to help finding it.
Then, you will find the download site for this jar. eg. http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
Then you download it and you include the jar in the classpath of your project. Or you can include the folder where all your jars are in the classpath.
If you find this all too complicated, you can also try to migrate your project to Maven which you will tell the name of the requiered dependencies (easily found online) and it will take care of downloading the jar and include it in your classpath.
I think there is no particular way to do it. If you grabbed the source code from an entire project, I would suggest you to look at the build script. The dependencies would be listed down there.
Else please search for with the FQCN on search engine.
For your imports, I did it for you and found out the source.
Apache Log4j 1.2.17
google-collections
Gson
JUnit
Hope this helps!

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

importing java subpackages on eclipse

Hi is it possible to import packages in java in similar way to relative paths?
For instance I have created my project in the package: com.test.calculator
Then I copied and pasted a folder in the com.test.calculator package, so the packages pasted look like this: com.test.calculator.org.apache.thrift.async and similar things. But now inside the java files in the folders, when they try to import org.apache.thrift.async for instance, it say that it cannot be resolved. Is there a way to make it notice that it is all under com.test.caclulator and consider the package path as a relative route?
Thanks

Categories

Resources