Creating a Java jar library to use on Android project - java

I am working on developing a game for Android and I have a lot of the code written on a java application. I would like to use the classes and methods in the Java application within my Android application. I am using Eclipse to develop my application so I tried to create a Jar for the Java application By doing: Right click project->Export->Jar I then saved the jar somewhere and added the jar to my Android buildpath. When I try to use methods from that jar I get compilation errors because Eclipse cant find the methods. Am I supposed to have any import statements? The name of the package on my java app is (default package) so I am not sure what the import statement would be. I would appreciate any help.
EDIT: I changed the package name of my application so that I could import the classes within it and that got rid of the compilation errors. When I try to run the project I now get NoClassDefFoundError Could it be that I am exporting the jar for my Java application wrong?

Open Project properties -> Java Build Path -> Order and Report tab.
Try to check jar check boxes,
see example:
To make sure that you created jar properly, here other snapshot of jar export:

Rename the package first. http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
Yes, you need to import the package inorder to use in a class. Else the compiler will not identify the codes. Import the jar into build path and import the package into the Java code as :
import java.util.HashMap;

If I understand your correctly you exporting a Java .jar (built using Java compiler)? As far as I know Android does not understand Java bytecode though the compiler for Android understands Java source code.
I'd suggest you just put all that source files in the Android project and compile them there, hopefully you aren't using functions that Android API does not have.

Related

Visual studio code cannot find java libraries

I've setup a Java project in Visual Studio Code. I installed previously a few java extensions :
and I can create a program that does Hello World.
In a folder called lib inside the project folder, I copied some jars (lwjgl3) and, although are shown by intellisense when I try to import them, I have an error that says it cannot find the package :
Anything I missed ?
You need to add the jar to your classpath. The classpath is something used by java to find the libraries your program uses. Java looks in each folder/jar archive to find the class files, because it would be slow if it searched every directory on your computer. See this question: What is a classpath and how do I set it?
Add a reference to the package in the following way.
Click the plus sign to the right of Referenced Libraries under the JAVA PROJECTS panel
Select the .jar file in the pop-up window.

Where do I find the source code for packages that I import in Java?

When I import a package, say e.g. import java.awt.Graphics, where can I find the source code for the Graphics class in my JDK installation on my Mac? I know the path to my installation is /Library/Java/JavaVirtualMachines, however I don't really understand exactly where the class is being generated from in the installation.
Thanks!
Assuming you are using a modern IDE, such as IntelliJ, along with a build tool such as Maven or Gradle, you should actually be able to just CTRL + CLICK on any method in the java.awt.Graphics package to view the decompiled source code. Developing using an IDE with something like Maven is recommended in most cases, so if you are not already doing so, now might be a good time to upgrade.

jasckson libraries in google app engine not included in whitelist

when I have these imports in my code or use any of the classes and and methods, I get this error from intelli J IDE.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Class 'com.fasterxml.jackson.databind.ObjectMapper' is not included in App Engine JRE White List
this does not result in any Maven build errors but or any errors in production. but why is the IDE flagging the error.
Class 'com.fasterxml.jackson.core.type.TypeReference' is not included in App Engine JRE White List
Those imports aren't part of the Java Application Programming Interface (Java API)
They are library that are created and in order for you to use them, you have to download and add the jar file in your java build path library

What are the steps to configure Struts 1.3 in Eclipse, with the open declaration and open implementation option on the code?

I've an existing project(in SVN Respository) developed using Struts1. Now I need to import it in the eclipse IDE.
When I try to import the project, I've 2 problems :
If, I place the files from my project to a newly created "Dynamic Web Project", its showing errors with all the javascript, jsp and java files(All import statements are shown as error)
If, I use SVN Checkout/Import, its working fine, no errors. But the links are not available on the java code, like when I click on a function/variable name, it should go the function/variable declaration/definition(Eclipse - Open Declaration/Open Implementation).
Someone please help on this.
I would always prefer to import projects as Maven projects (if they are) so that it would downloads the dependencies on the go.
If the import statements are not recognized means, you should configure the corresponding JRE. This may help you.
Finally, there are so many aspects while configuring an existing project. Please Check your project Wiki if any. If not do create one while you short out them.

How do you import and use existing libraries in Eclipse?

I'm Visual Studios / C#, you can simply add a reference to a DLL file and then just use that namespace. I'm starting to dig into Android development and want to use the SimpleFTP library. Downloaded the .jar file, went to File --> Import and now I've got SimpleFTP.class and SimpleFTP.java in org\jibble\simpleftp in my Package Explorer.
But if I try to import org.jibble.simpleftp; it says it cannot be resolved. What have I done wrong?
I was hoping I could just start typing as if I had properly imported the library and it'd figure out how to correct my package, but no such luck =[
There are at least three solutions:
If you have the source code, you can place it under your src folder directly in your project.
If you have the source code, you can create an Android Library Project with the source code and connect your Android app projec to the library project.
If you have a JAR file, you can place it in the libs folder of your proejct.
Project -> Properties -> Java Path -> Libraries. There you can add your existing libraries, in several formats (jar, .zip, etc.).
Right click on your project and add build path

Categories