Trouble with a project with imported libraries dependent on other packages - java

I'm fairly new to Android development and I'm having trouble with external library dependencies at the moment. In particular I've imported a jar file that has upstream dependencies on another package which it can't locate. The package I imported is trying to import java.time.format.SimpleDateTimeTextProvider. I found a jar which apparently contains this package, but when I add it to my project the error still appears. When I try to import the same package into my own source code for the main activity, there are no errors, so it clearly is a standard part of Java and available somewhere on the machine I am working on.
Is there a way to force imported packages to look for packages they are dependant on by searching the rest of the machine? Alternatively, is there another common way of dealing with this? Lastly, does someone know (just in case there is something wrong with the package I've already tried) where I can find a jar file containing java.time.format.SimpleDateTimeTextProvider?
Thanks in advance.

Related

Importing vanilla Java project to use as a library in Android

I've written a piece of vanilla Java code to do a specific task. It contains a bunch of packages and resource files.
I want to import the vanilla Java project to Android to use some of its utilities.
I've tried to build my project to jar file, add it to the libs/ folder and import it. I've also tried to import it directly to the project dependencies. However, none of the above worked. The jar file showed up in the "External Library" part, but I cannot import it from a class inside as it gave me compilation error.
HERE you have plenty of ways how to attach jar to Android project
are you shure that your jar (assuming properly built) will work on Android? not all components and classes of "vanilla" Java are available on Googles OS... check out THIS topic for more info

Creating a JAR from code WITHOUT a main class

I am using IntelliJ and I would like to package a library I found into a jar for my project. Said library was found online and only contains java files without a main class. Before someone flags this for being a duplicate, it is not. All other questions are with projects that have a main class. I tried following https://stackoverflow.com/a/45303637/10002144, but it does not work because I get to a point where it says "META-INF/MANIFEST.MF file not found in 'libname.jar'. I also tried making a gradle project and building it, but it simply creates a jar that is only 297 bytes long, so obviously that doesn't work.
What can I do in my case?
What are you using as a package resolver?
For maven, gradle, sbt projects you can simply run the package instruction (i.e. mvn package) and it will jar your project regardless of a main class.

How to include java src code so i can use it inside Android studio

I have found some src code that I want to use in my projects in Android Studio. It contains some java code that I need for my gradle project. I tried to import it but it doesn't work at all. Here is that folder with src classes.
Does anyone know a solution?
If it's your source code (or a library you've downloaded that has the source code) then can just copy and paste the files into a directory within your project and add them to source control. You'll need to open the files and make sure the package names are correct (matching the path that they are put into). This assumes that the library only depends on core Java features and not other libraries (otherwise you'll need to manually copy and paste all their source or build them and bring them in as dependencies). Honestly, seeing as you are using gradle you should completely ignore this method.
Another way to do it would be to compile the source code to a .jar file (using Java compiler: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html) and then import the produced .jar as a dependency to your project.
Or, even better, just open your modules gradle file and in the dependencies section, add this: compile "com.badlogicgames.box2dlights:box2dlights:1.4" then sync the project. Boom, now you have the library as a dependency so you can use all it's public classes etc. And you didn't even have to do any compiling, copy pasting etc. The power of gradle!
Tip: Here is what I am guessing you did. You researched the library or heard about it somewhere and came across this page: github.com/libgdx/box2dlights. Then you didn't scroll down, instead, you clicked clone or download. This is where your confusion started. Because it's open source, yes you can download and build it yourself (or download and modify it, which is why you usually clone a repo). Instead, in general, scroll down and you will see how to include libs through a simple 1 liner in gradle.
Hope this helps!

Cannot resolve json import Intellij after adding dependency and library .jar

I cannot seem to resolve this issue after adding the json-lib-2.4-jdk15.jar from an online source to the project dependencies, screenshots provided to show walkthrough of what I am doing. (and yes I have repeatedly tried "Invalidate caches and restart")
The error:
This is my Project Structure ("cmd + ;" or "File > Project Structure")
It doesn't seem to work on "Scope" set to Compile either. Note that I added the .jar file as a library too because I'm just really unsure as to why I can't get this to work. Any help is greatly appreciated, thanks.
What do I seem to be doing incorrectly here?
The jar you have downloaded from https://sourceforge.net/projects/json-lib/ doesn't contain the classes you need. This jar contains net.sf.json package which is a different implementation of JSON Java library.
You are trying to use org.json.* classes which are provided by another library. The library you want to use is this one: JSON-java. At the bottom you can find the link to the Maven repository where you can get the jar file.
The latest version (at the moment of this reply) direct download link for the jar is here: json-20160810.jar.
Add it to the module dependencies and your problem should resolve.
Next time you observe similar issue browse inside the library jar under External Libraries node of IntelliJ IDEA project view and see what classes it contains and under which packages. This way you would know if the issue is caused by the wrong jar you've added to the dependencies and which doesn't actually contain the classes you want to use.

how do I add open source API code to an eclipse project?

I have to write a java application which I'm putting together using eclipse and it relies on open source code. This application needs to be self-contained, meaning that I'm supposed to create a jar file that has no external dependencies.
I can use the open source code when I reference the jar files in the project's build path, but the idea is to have the actual source code as part of the eclipse project, side-by-side with my code.
The source code can be found here: http://hc.apache.org/, but when I import an existing file system into my project I can't quite get things to work. The packages end up with the wrong names, breaking references, and I can't do anything. Notice that the folder containing the source code has this structure:
httpcomponents-client-4.2.3\
src\
httpmime\
httpclient-osgi
httpclient-contrib
httpclient-cache
httpclient-benchmark
httpclient
fluent-hc
each of those subfolders has src/main/java/org/apache subfolders.
Can someone please explain how to do this? Am I supposed to import everything one java file at a time?
Use a tool like OneJar, FatJar, JarJar, etc. to create a single-jar application.
As Charlie mentioned, the Maven Shade plugin is another choice, particularly if you're already using Maven. If you're not, consider it or another transitive dependency management tool.
Some tool should be used, IMO, and it's more important the more dependencies you have.
Alternatively you could use a jar class loader and include the jar file in your artifact.
I would most definitely not include the source of dependencies in your own project.

Categories