Import Java Project, not found src - java

let me tell you that I'm new in Java and I'm having this problem with an Android App.
The app is made by two projects, the launcher activity(Project A) and another one (Project B) which has some common SRCs for multiple Android Apps (like http connection classes, etc).
I imported project B from A, and in my .java files of my project A, I could import without problem src from B (import path from B;)
But when I'm debbuging and it has to use a file from B, I get an NoClassDefFoundError with the path of some file in project B.
After that, my app fails and closes.
Sorry if my English is complicated. Hope you understand my problem and can help me.
I guess it's something about a missing step in my import.

Importing just puts these projects into your workspace. If they are 2 separate projects you should import them as separate projects. Either right click import and navigate to them on your file system or if they are already in your workspace, do create new java project and give it the same name as the directory of the project (The wizard will make it clear that it recognizes a java project exists in that folder).
Then check to make sure that the package names and imports one package to the other are correct. If they weren't you should errors marked on those lines.
Finally make sure that the project which you are actually running from has the other project on its build path. Right click on the project, configure build path, and add the packages. Alternatively you can use something like maven to automate the installation of this second package as a jar which will automatically update in project A when project B is changed)

Please make sure you include the classes/src from project B in your project A's build path , by right clicking on your project A "Build Path" -> "Configure Build Path". Eclipse automatically will add those classes to your class path too.
The NoClassDefFoundError usually occurs when the classes needed to compile the project are present during build time but missing during run time.
Hope this helps !!

To reference other projects source in current project, just import them into your current project by adding them to your workspace. This link has some neat steps to accomplish this. You can follow which one suites you.

Related

How to call method/variable of one eclipse project to another eclipse project?

I have two Projects in eclipse and I simply tried to call method of one project to another but not able to do it even if I set up the whole flow of project setup using properties and even classpath.
I have project CacheProject which contains CachePackage as a package which has CacheMain.java class. This CacheMain class contains a public method getName() which should return me a value of one of the varible there as string.
I have also created jar for the respective CacheProject and added to classpath of another project CacheConsumer1. also I have added project CacheProject to Right click on CacheConsumer1 -> properties -> java build path -> projects.
Still I am neither able to access CacheMain nor getName() method of it:
also I am not able to access anything from the CachePackage:
Please help with it guys if anybody has already worked with something like this. Because none of the earlier answer posted in this stackOverFlow post is working for my scenario.
Actually everything would be easier if you would learn to use Maven or Gradle in order to build your projects and define dependencies. But in Eclipse you can just open both projects, then go to the project properties of project CacheConsumer1 and set project CacheProject as a build path dependency:
This will also lead to CacheConsumer1 automatically being rebuilt as soon as there are changes in CacheProject.
Update: I just noticed in your screenshot that CacheProject has no src folder or any other folder marked as source folder (the folder icon with the little parcel in it). Maybe you just start over, create a new Java project with default Eclipse folder structure and move the class or classes over to that project, creating your Java packages and classes inside the src folder.

NoClassDefFoundError in nested jar (Android)

I have created simple app to test the scenario which is failing with NoClassDefFoundError.
Lets take for example i have Test1 android project with Class TestClass with some methods.
This Test1 project i exported as a test1.jar and exported only source folder with Class TestClass and classpath and .project files.
In another android project Test2 i added this test1.jar in my libs folder. And in this Test2 project i have a class Test2Class which calls methods of test1.jar class.
After this i exported this Test2 project as test2.jar file following above steps.
So when i use this test2.jar in another project i get this above error NoClassDefFoundError. It is the scenario of jar inside a jar.
Is anywhere should i do something so i am able to access a jar inside a jar.
Thanks in advance.
Try this Step by step:
1. Remove all library projects then Clean.
2. Go to first Project Test1 Right click Properties -> Android. Check Is Library(make it library).
3. Go to second Project Test2 Right click Properties -> Android in the Library section select Add -> Add first project as library.
4. Go to second Project Test2 Right click Properties -> Android. Also, Check Is Library.
5. Go to third project C Right click Properties -> Android. In the Library section select Add -> Add First and Second as library. (Do not make Proj C as library)
6. Now Clean everything and run Proj C.
Edit:
You can also see this bug Bug 405212 - Do not pack jars bundled inside a packed jar and Bug 361628 - Nested jars cannot be unpacked with Java 1.7. Try the above way it should work.
Remove the jars from the java build path. Add new folder name as "libs" and posted all the jars in that folder. Application will run fine
Try this :
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.
Mark all checkboxes and Click on Apply and clean the project.
It cant be done. You will need to unzip the JARs and add them to the classpath to expose the nested JAR. See this Java bug for reference.
There are several alternatives to handling it in a proper way, see this question for example, which recommends One-jar, Uber jar or Shade
Note: This is relevant for Java, Andoid might behave different, but it seems like it has the same issue.

Android java.lang.NoClassDefFoundError after import JAR

I got a problem...
I made a Jar (called "GeoFence.jar"), that I have to use on a Android Project.
So, what I done is:
Look In so many webs HowTo's
create "libs" folder in the android project.
add my JAR (manually)
then I went to Java Build Path and checked that my JAR was included to the Android Dependences.
finally I made the necessary Imports to use the class I need from JAR.
After do all of this, I runned my App, and it throws this error:
and I don't understand what's going on, cause, I Fixed the Project Properties and cleaned it. And it's still not working.
Here is how i have the structure and the build path.
for adding jar use below two stpe: but for you just do 2nd.
1. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
2. Your Project -> right click -> Properties -> Java Build Path -> Order & Export --> Select All --> OK
After taht just clean your Project and RUN.
If you go to you Android Dependencies folder, you should be able to expand the GeoFence.jar file. This way, you should be able to find the class com.elias.polygon.City if you keep expanding subfolders.
If you CANNOT see it, you probably made an error in exporting the jar file.
To solve the export see this question on so
If you CAN see the class, you have an error in your build path/ project properties
To solve the project setup, see this question
the problem that I had, was that the jar I wanted to import, used some packages that in android are not "allowed" so I had to do in a different way.
After that, it happens the same, but the problem was that the Jar was Build in v1.7.
When I builded in v1.6 android accepted it.
So, first of all look for:
Don't use packacges that in android are not allowed
Look in which version is builded the solution.

Adding project reference to java in eclipse

What I'm doing seems to me quite simple but it's just not working out. This has a lot to do with me being a novice to java and eclipse.
I want to reference another project(call it javalibproject) from my eclipse project (call it mainproject) . So i add javalibproject to the projects tab in in the java build path of mainproject and also add a project reference to it.
What i was hoping at this stage is that i would be able to import the javalibproject package and declare one of the referenced classes. But in my class in mainproject, i get javalibproject cannot be resolved to a type. I would have also expected some sort of library folder displayed in package explorer showing javalibproject (similar to what happens when you reference an external jar).
Any ideas what i'm doing wrong? WOuld i be best off trying to export javalibproject as a JAR and reference that.
Thanks,
Eoin
You need to add that project to main project build path.
On your main project -> properties -> java build path -> projects -> add.
For second solution (add it as jar) look at Apache Maven as it will save you lot of time.
We can certainly have all librariers at one place and use that as project refernce in main project. What might be missing though is some correct configuration of javalibproject. Is it working right as stand-alone?
That will not be shown as other libs are shown in your mainproject.
What you are trying to is also quite right though the best way to manage the jars and library dependencies is through maven/ ant+ivy.

Netbeans and source folders

I am working on a large project and I have configured multiple source package folders.
Project -> Properties -> Sources
However, when I import a library from this other source folder it says it doesn't exist so obviously with source folder is only liked in during compile time.
How do I get my project to reference these source files to work within my project without creating class files etc, and keeping both source directories separate from one another?
Thanks
If I understand your question correctly, you wish to make the source files from one NetBeans (Java Application) project A visible in another like project B.
In project B:
Go to Properties >> Libraries
Go to the Libraries category
Go to the Compile tab
Choose 'Add Project...' and select project A.
Regardless of whether the sources in A have been compiled, you should then be able to use them (e.g., name auto-complete, javadoc peek) when editing source code in B.
Make sure package names are correctly defined!

Categories

Resources