Include non-android project in android project (lib or classpath) - java

I'm trying to import a non-android project into my android-project in eclipse. I've tried everything, but nothing works.
I've added the project into my classpath -> noclassdeffounderror
I've created a .jar from my non-android project and moved it into "libs" -> same error
I've tried to "import an external jar" -> same error
I've searched the whole internet for getting this done, but I don't know, why I'm still getting this Exception by runtime.
Hope anyone has a good idea :(

Go into the project's Properties, select Java Build Path, select the Order and Export tab, and check the box next to the JAR file you're including.

Related

Cannot import ClassPathXmlApplicationContext

I learning Java Spring Framework by listening to
the "Spring & Hibernate for Beginners" udemy course.
I struggled while trying to
import org.springframework.context.support.ClassPathXmlApplicationContext;
Eclipse shows me the error:
ClassPathXmlApplicationContext cannot be resolved
The author of the course to which I'm listening is still not involving Maven
(and pom.xml) because he is concentrating on
"pure" Java and Spring in his course,
so please don't direct me to use Maven for organizing the project.
I added all jars from spring-framework-5.0.2.RELEASE-dist to my projects buildpath.
The funny thing is that when i do CTRL+Shift+O Eclipse automatically imports
the org.springframework.context.support.ClassPathXmlApplicationContext package,
but it shows error in import line (red line under org)
and shows an error in my main function
on the line where I try to use context as:
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Please help.
I just want to share that ะจ have found a solution to my problem.
My execution environment JRE was set to be JAVA SE 10. After I change it to be JAVA SE 1.8 everything started working, and no errors are showing now.
I do it like this:
Right click on your project, then open Properties
Java build path
Click on Add Library
Choose JRE system library
Click on environments and choose JAVA SE 1.8
After that, I removed JAVA SE 10 from my build path and everything becomes right.
Delete the module-info.java file in your project, It's only used if you're using Java's built-in module system.
Hope that helps:)
I would check the following:
a. right click the project -> properties -> Java Build Path -> Libraries (tab)
Make sure spring-context jar is present and there is only one version of it.
If that is the case, try closing and reopening the IDE.
I solved this problem in intelliJ by doing the following:
File > Project Structure
Modules
Dependencies Tab
Click (+) button
Add JARs or Directories
Navigate to the correct .jar to import ClassPathXmlApplicationContext
Because I have been using maven in the past, I already had the .jar file downloaded. My .jar file was at the following location on my drive:
home folder
Unhide hidden folders
.m2 folder
repository > org > springframework > spring-context > 5.2.4 RELEASE
Select .jar file
I also had to import the "spring-beans" and "spring-core" libraries to get the code to run in my environment.
Step 1: Right Click your Project --> move mouse to "Build Path" tab
Step 2: Choice "Configure Build Path"
Step 3: on tab "Libraries" and click "Classpath". If "Classpath" is empty, it mean you still not add .jar and can't import ClassPathXmlApplicationContext.
How to:
create a new folder equal level src. i suggest name folder is "lib".
Download .jar file and copy to folder lib.
Follow step 3 above and click "Add jar" on right tab and choice .jar file on "lib" folder.
Finish and enjoy!

Update External Jar in Android Project

All, I have an external JAR that I'm adding to my Android project. However, as my jar is consistently updating and changing. When I update my JAR, I get the dalvik VM magic number error.
I don't understand how I'm supposed to fix this?
Here's the steps I've followed.
Adding The Jar Initially
Copy JAR to libs folder in my project
Run project
My JAR is picked up, installed everyone is happy.
Updating the Jar
In Eclipse, go to the jar in libs and right click -> delete.
Copy my new jar (same name) into the libs folder.
Receive bad class file magic error.
My Troubleshooting So Far
In eclipse, Project -> Clean...
Select my project, clean it.
Run it. Same error... magic number
Project Properties -> Java Build Path -> Remove Android Private Libraries
Now the private libraries aren't being re-added. (Which equates to missing imports in my Java project) Why is this a nightmare? Any suggestions?
Turns out that my issue was caused by the multiple JDK's I had. Android was defaulting to use JDK 1.7 while my console jar was using JDK 1.8

Importing an existing project to eclipse is giving me a lot of errors

I imported an existing android project and I just can't get it to work. I'm pretty sure the problem is here
At first, all 3 of those has a red cross at them but I fixed android-support-v4 and google-play-serives, but I just can't find google-play-services_lib anywhere, it's supposed to be in the google-play-services/bin folder, but I don't have that folder, I only have libs folder.
Another problem is also that when I try to open ANYTHING in the project (for example, android manifest or any .java file) it throws me this error.
Any idea how I could solve my problem?
Remove all jar files from java build path and follow these steps
You should try this:
Remove all references to the JAR in your project from Java project -> properties -> Java build path -> libraries
Create a libs folder if not exist at the root of your project Copy the JAR into the libs folder.
If still not running OK. Right click your project > Android Tools > Fix Project Properties
clean your project and run. it will work
Seems like you are missing google-play-services-lib from your project.If you cant find it then please refer to this question
https://stackoverflow.com/a/14137511/1238731

Including a Jar in Android App

I want to include a jar into my Android package.
Unfortunately, I can't get it to work. I followed several explanations, such as this one, but I still get NoClassDefFoundErrors - at runtime, building, compiling, installing the project worked without errors.
Most answers seem to be outdated. How do I solve this issue using the current Eclipse, ADT and Android versions? Adding them to the Java Build Path like in plain Java projects didn't help.
All help appreciated.
What I have tried
Putting them into a folder and including into the project [Screenshot]
Put the jar in a folder entitled 'libs' (should be in the root of your project. Then add it to the java build path.
If that doesn't work try this:
Your Project -> right click -> Import -> File System -> yourjar.jar
Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.ja
Add your jars to libs folder. Automatically add's those JARs to your compile-time build path. More importantly, it will put the contents of the JARs into your APK file, so they will be part of your run-time build path.
You two things and the error will be fixed
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 3rd tab from top i.e "Libraries" -->Click Add Jar--> Select your Jar
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 4th tab from top i.e "Order and Export" --> Select(chekbox) your Jar.
For more help check this link

Android import library

In my project, I need to use external library. That library consists of .class files. I have added folder with this library to my project using properties -> Java Build Path -> Libraries -> Add class folder. I can compile project without any problem, but when I run it, I get failed resolving xy and debugger stops on line where I create instance of class from imported files.
I guess the external library I use isnt linked to my .apk file. How can I fix this? I have tried to check this library on "Order and Export" tab assuming that, it will export the library with my application. But it didnt work.
How can I link this library to my project .apk? Do I need to somehow install this library on device first? Do I need to copy this library in application folder?
PS: I'm using Eclipse Juno, android SDK target 10.
Thank you
UPDATE
I have this library copied in libs folder. External library path is:
my_project/libs/sk/aicit/leg/libraries*.class
I have added ./libs folder in Libraries settings of project.
I do not have .jar version of this library, only .class files.
I have also souce code of this library, but I didnt want to include its classes in my project, it has its own dependencies, I wanted to just "link" it like library. Its an external library I havent written.
UPDATE 2
I have copied all the source files from library to my projects "src/" dir. I have removed Library from Java build path. I can stil compile my project, but when I run it Iget
FATAL EXCEPTION:main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{
com.example/myvideorecord/com.example.myvideorecord.Robot}: java.langNullPointerException
...
What can I do, how to debug this error? :(
steps
Copy the library file into libs folder
select jar file -> right click -> Build Path -> Add to Build Path
clean the project Project-> Clean -> select your project -> ok
Android Studio
For importing a Android library into Android Studio, use the following steps:
1) (Optional) I like having my main Android project contain everything it needs to compile correctly, so I move external libraries into the libs/ directory. Generally, you only need to keep the following directories:
res
src
AndroidManifest.xml
*.iml (This will typically be the name of the library you're importing.)
2) Go to your Project Properties.
3) Go to Modules.
4) Import a New Module (Command + N and then Import Module).
5) Navigate to the library directory you want to import.
6) Create module from existing sources.
7) Click Next three times to add the necessary files.
8) Click Finish to complete the module additional.
9) Click on your project in the module list and go to the Dependencies tab.
10) Click the + button at the bottom and click Module Dependency....
11) Make sure your library you're importing is selected and click OK.
12) Click OK in the Project Properties window.
And you should be good to go.
Copy that library file into libs folder
if u have the source code of the library you can import it in eclipse .
Right click on your project -> properties->android -> reference-> add
and add the library project which u have imported. This will include only .class files of the library in ur project

Categories