importing java subpackages on eclipse - java

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

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

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

import package statement not working

I am trying to import a package into another project. Just to be clear, I am using NetBeans and all my projects are located in the same folder.
So, I have a package called createobjectdemo, and I am trying to import it into another project called valueOfDemo.
The statement that I am using is:
import createobjectdemo.*;
This line invokes the error: package createobjectdemo does not exit.
But... it does exist! It is located in the same folder hosting all my Java projects. I have it open right now in the NetBeans editor window.
Why can't I access the package? Do I have to provide a specific path? Sorry, this has got to be really simple, but I can't figure it out.
Thanks a lot.
You need to make .jar file of java files which you want to use in another project and add your project in menu Libraries to another project.
Not knowing exactly how your projects are set up makes it difficult to answer your question. I imagine your setup is something like the following:
projects/
project_1/
src/ // Or maybe you have src/ and classes/ in the same directory
createobjectdemo/
CreateObject.java
classes/
createobjectdemo/
CreateObject.class
project_2/
src
valueOfDemo/
ValueOf.java
classes/
valueOfDemo/
ValueOf.class
So, the package exists but it's not in your class path. You can tell NetBeans (I have no experience with it, but I'm sure you can) that you want project1's classes directory added to your build path, or possibly just add the first project to the build path, and NetBeans can figure out where to find the compiled classes.
Remember, each file in a package must declare that's it's in that package; otherwise, it just happens to be in that directory but (if the directory is on the classpath), it's in the "default" package, and can't be imported by package name.

How do I use classes from an imported library in netbeans (java)

This must be the simplest thing. I have been trying to do this for hours. I know how to import libraries into netbeans but how do I use classes in these libraries using imports? I have been trying unsuccessfully for hours to use the javacv library on 64bit linux and when I try to import the necessary files from the library netbeans tells me the package does not exist. I want to access a java file example.java inside com.googlecode.javacv.cpp.linux-x86_64 which is inside opencv-2.4.8-linux-x86_64.jar. How do I actually import this so I can do the following: example obj = new example?
Also, in almost every reference to javacv I see this import com.googlecode.javacv.CanvasFrame; but nowhere have I actually seen this in any of the files readily available by google. What's the deal?
Please give this a try, I will be as specific as possible:
Copy the library folder you want to use
Paste it in the "src" folder of your project folder
Once you completed the above two tasks, you can import different classes from the library using the following code:
import nameOfLibraryFolder.*;
The above code will import all the classes from within the library. Replace "nameOfLibraryFolder" with the name of the library folder, to be more specific, the folder that contains all the classes of the library, to be even more specific, the folder that you placed in your project's subfolder called "src".
In order to import a specific class from the library, you need to replace the * symbol with the class name.
For example:
import nameOfLibraryFolder.className;
but if the class is in a subfolder, then:
import nameOfLibraryFolder.subFolderName.className;
I hope this is specific enough, and I hope it helps.
In netbeans, it is rather easy to iomport a external jar and utilize it.
First you need to add jar file in project tab by right clicking on the library node.
Then, in this project, just import the.name in your java file.
This should work, I have used Netbeans for three years
You many need to do one of the following option.
Add an new Library to NetBeans, then add it to you project
If you Tools -> Library a popup will come up.
There you can add a new library. Click on New Library and name the new Library.
Import a library from the ClassPath tab Add Jar/Folder
If you want to can also add the javadoc from the JavaDoc tab. You would add the file from the library file you downloaded, called javadoc. This will allow for the javadocs to be seen while editing your code.
Also you can add the sources file form the Sources tab. Import the file named sources
Then click on the Libraries folder icon in your project and Add Library
Another option is just add the .jar.
Right-click on the Libraries folder icon in your project and click Add Jar
Locate the .jar file and add it.
As long as you have:
package MyProject;
At the top of your project Java wont recognize it.
* It seems to be like some sort of sandboxing affect.
* To use your classes you have to open the directory you
* Saved the project to and Copy/Paste the library to the source file.
* I also found that simply deleting the named package works as well.
* But now you're no longer linked to that package. -shrugs-

Unable to use existing Java packages in Eclipse?

I'm learning Java from Bruce Eckel's Thinking in Java. After reading about packages early through the book, I thought I had got it, but when I decided to use the supplementary code(in the form of a single ZIP), I'm not so sure anymore.
The ZIP file contains packages in the correct hierarchy. I've an extracted copy of the ZIP too. Let's call this folder TIJ4. Both the ZIP and the folder are located on my Desktop. I'm working with an example Shapes.java which has an import statement like this:
import polymorphism.shape.*;. I imported this folder in the Project Properties>Libraries>Add external class folder. The strange thing is that Eclipse does not show an error on the import line, yet it cannot resolve names to types. I could manually create a package, then a class from within Eclipse, but isn't there an easier way to do this?
I've gooogled about this but nobody seems to have the exact problem as me. Here's what I've got so far:
1)New Java Project created. Created a class file Shapes.java in the project:
2) Project Properties>Libraries>Add External Class Folder. Selected my folder TIJ4. Notice that the error on the import is now gone. Apparently Eclipse has found the polymorphism package.
3)But I still get XXX cannot be resolved to a type errors all over the place!
what do I do? I also tried Project>Build Path>Add external archives and selected the ZIP, however, ended up in the same place.
As the Zip file contains only Java sources, you will have to compile them. Simple way, right now, is extract them into the src directory in your eclipse project. That way they will be compiled and automatically included in the classpath.
The reason for not able to resolve the classes even the import error goes away, is the zip file contains the directories that satisfy the package structure but there are no compiled classes (the .class files) are found in them.

Categories

Resources