How can I bundle a .so file in a jar? - java

I'm currently working on a java library (binding) which uses some own written native code. This native code is compiled as a .so file for multiple architectures (arm-v7, i686, x86-64, etc).
I know in android you have to create a folder called jniLibs with subfolders for each architecture containing the proper .so file. Then with an Android.mk file and System.loadLibrary I can include these files into my code.
However, I have no clue how to include these .so files in a normal java project/library. I have read online that System.loadLibrary only works for looking through normal files (and not necessarily project files).

You can't have anything other than code in an android .jar file, however, you can have resources in an .aar file.
Android Archive Library (aar) vs standard jar
You will find that the .aar for your library is the redistributable/reusable compiled version of your library.

You can do it as how SWT did it -- extract the library files from your .jar file to user's home directory (so you are sure you have access to write in it) manually by java code.
You can check the source code of org.eclipse.swt.internal.Library, method extract().

Related

How compile a java file to .class who use others .class. (if it's possible)

I get lib webrtc in a .aar, and I need to modify one file of this lib.
We can call this file tomtom.class for this example
So I unzip this .aar to get many .jar and in those .jar there the file tomtom.class who need to be replaced.
I have the new version of file tomtom in java because I edit it. But now I need to compile it for replacing the old tomtom.class. Of course, this file uses other classes inside this lib webrtc.
So is it possible to compile a java file who use other compiled file?
After many tried of use classpath. Or other method I find on Internet.
None work, maybe it's me who is a moron but whatever..
How I replaced my file on a lib already compiled (it's the webrtc lib).
I put a copy of the file decompiled in my project on android studio. I can modify it like I want. And this decompiled file can compile with the lib I had in the folder lib.
I build the project and I go to the temp file to get the compiled file.
Then I can replace the one in .aar by the file compiled I just modify.

Android: How can you "unpack" or "dump" files from a library project into the main project?

I would like to "dump" or "unpack" a library project's files into the main project, which would let me customize the library's contents for that project. Is that possible, and what is a way to do so? Can I just copy the entire library project's src/, res/, and manifest file into the libs/ folder of the main project?
(Please note that I do not want to touch the files from the original library project because I have other projects that are using it.)
If your library is a .jar containing .java source files, then you can unpack it using, eg. unzip, and move the contents of its src and res directories into your project's src and res directories.
If, however, this library contains only compiled Java .class files, there's not much you will be able to do with them. Unless you want to try to decompile them, the library must be used as-is.
I think that is better change the project name of the library and make a copy of this. This allow you to modify the files and dont mix it with your actual project.

jsr305_annotations Folder inside apk file

I've had reports of problems with the installation of the apk.
I tried to extract the apk as a zip file and I found the folder
jsr305_annotations in the root APK
with inside
folder: v0_r47 -> file: V0_r47.gwt.xml
File: Jsr305_annotations.gwt.xml
I would like information about those files and folders and a method to make sure that they are not included in my applications
thanks
Using
Eclipse
Version: 3.8.2
Build id: M20130131-0800
Android Development Toolkit
Version: 22.0.1.v201305230001--685705
I'd seen those files included in an app as a result of including the library GoogleAdMobAdsSdk in a project (as available from here https://developers.google.com/mobile-ads-sdk/)
If you open that JAR file you can see those annotations folders and XML files are present.
If you don't include that library directly, you may be including another JAR which has those files.
I'd suggest checking the JAR files you include in your project (by opening them in a file that can open APK/Zip files) to find which one(s) include the file and look for an alternative library.
The javascript that comes with it looks like it will lock you out of your device and start making phone calls, mail, SMS, etc while it plays audio and shows a video. It also creates a *.css file and calls on varible uri. I didnt inspect every file but from the *.js file it dont look like I would want to run it. I found this in my Downloads folder, file name, "yourdownloadisready.apk", Im a tech. I cant imagine if a novice saw this.

What exactly does a jar file contain?

As an intern, I use company code in my projects and they usually send me a jar file to work with. I add it to the build path in Eclipse and usually all is fine and dandy.
However, I got curious to know, what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
What does this mean? I come from a C/C++ background so is a jar similar to an already compiled .o file and all I can see is the .h stuff? Or is there actual code in the jar file that I'm using that's encrypted so I can't read it?
Thanks for all the answers!
Edit: Thanks, guys, I knew it was a sort of like an archive but I was confused to why when I tried to open the .class files, I got a bunch of random characters. The output was similar when I tried to open a .o file in C so I just wanted to make sure.
Thanks!
A JAR file is actually just a ZIP file. It can contain anything - usually it contains compiled Java code (*.class), but sometimes also Java sourcecode (*.java).
However, Java can be decompiled - in case the developer obfuscated his code you won't get any useful class/function/variable names though.
However, I got curious to what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the jar itself). It's hard to compare C to Java really, as Java byte code maintains a lot more metadata than most binary formats - but the class file is compiled code instead of source code.
If you either open the jar file with a zip utility or run jar xf foo.jar you can extract the files from it, and have a look at them. Note that you don't need a jar file to run Java code - classloaders can load class data directly from the file system, or from URLs, as well as from jar files.
The best way to understand what the jar file contains is by executing this :
Go to command line and execute jar tvf jarfilename.jar
A jar file is a zip file with some additional files containing metadata. (Despite the .jar extension, it is in zip format, and any utilities that deal with .zip files are also able to deal with .jar files.)
http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html
Jar files can contain any kind of files, but they usually contain class files and supporting configuration files (properties), graphics and other data files needed by the application.
Class files contain compiled Java code, which is executable by the Java Virtual Machine.
http://en.wikipedia.org/wiki/Java_class_file
JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.
Jar file contains compiled Java binary classes in the form of *.class which can be converted to readable .java class by decompiling it using some open source decompiler. The jar also has an optional META-INF/MANIFEST.MF which tells us how to use the jar file - specifies other jar files for loading with the jar.
Jar( Java Archive) contains group of .class files.
1.To create Jar File (Zip File)
if one .class (say, Demo.class) then use command jar -cvf NameOfJarFile.jar Demo.class (usually it’s not feasible for only one .class file)
if more than one .class (say, Demo.class , DemoOne.class) then use command jar -cvf NameOfJarFile.jar Demo.class DemoOne.class
if all .class is to be group (say, Demo.class , DemoOne.class etc) then use command jar -cvf NameOfJarFile.jar *.class
2.To extract Jar File (Unzip File)
jar -xvf NameOfJarFile.jar
3.To display table of content
jar -tvf NameOfJarFile.jar
A .jar file is akin to a .exe file.
In essence, they are both executable files.
A jar file is also a archive (JAR = Java ARchive). In a jar file, you will see folders and class files. Each .class file is similar to a .o you might get from C or C++, and is a compiled java archive.
If you wanted to see the code in a jar file, download a java decompiler (located here: http://java.decompiler.free.fr/?q=jdgui) and a .jar extractor (7zip works fine).
JD-GUI is a very handy tool for browsing and decompiling JARs
A .jar file contains compiled code (*.class files) and other data/resources related to that code. It enables you to bundle multiple files into a single archive file. It also contains metadata. Since it is a zip file it is capable of compressing the data that you put into it.
Couple of things i found useful.
http://www.skylit.com/javamethods/faqs/createjar.html
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
The book OSGi in practice defines JAR files as, "JARs are archive files based on the ZIP file format,
allowing many files to be aggregated into a single file. Typically the files
contained in the archive are a mixture of compiled Java class files and resource
files such as images and documents. Additionally the specification defines a
standard location within a JAR archive for metadata — the META-INF folder
— and several standard file names and formats within that directly, most
important of which is the MANIFEST.MF file."
Just check if the aopalliance.jar file has .java files instead of .class files. if so, just extract the jar file, import it in eclipse & create a jar though eclipse. It worked for me.
While learning about JAR, I came across this thread, but couldn't get enough information for people like me, who have .NET background, so I'm gonna add few points which can help persons like myself with .NET background.
First we need to define similar concept to JAR in .NET which is Assembly and assembly shares a lot in common with Java JAR files.
So, an assembly is the fundamental unit of code packaging in the .NET environment. Assemblies are self contained and typically contain the intermediate code from compiling classes, metadata about the classes, and any other files needed by the packaged code to perform its task. Since assemblies are the fundamental unit of code packaging, several actions related to interacting with types must be done at the assembly level. For instance, granting of security permissions, code deployment, and versioning are done at the assembly level.
Java JAR files perform a similar task in Java with most differences being in the implementation. Assemblies are usually stored as EXEs or DLLs while JAR files are stored in the ZIP file format.
Source of Information -> 5- Assemblies

adding dll library to java using idea for using a program

I am trying to add and use a program called JVLC to my program. I downloaded a zip file that contains a jar file(jvlc.jar) for java interface and 2 dll files (jvlc.dll , libvlc.dll) and a folder that contains many dll files. when I run my program an UnsatisfiedLinkError occurs.
I used this code to add those 2 dll files to my project.
System.loadLibrary("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\jvlc.dll");
System.loadLibrary("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\libvlc.dll");
but still there is error:
UnsatisfiedLinkError: Directory
separator should not appear in library
name
Is it necessary to add all folder to library paths? If yes how?
please guide me.
The System.loadLibrary method loads a libary based on a library name (libName, without extension) and not through file name. Example, Java comes with a zip.dll / zip.so (Linux) that is used when we use the Zip Deflater/Inflater classes for zip files.
If you want to use specify a dll file name, use the System.load(String filename) method otherwise, register your DLL in a java lib path.
An example can be found here.
For your example, please do this:
//Your code....
System.loadLibrary("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\jvlc.dll");
System.loadLibrary("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\libvlc.dll");
//Replace with this...
System.load("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\jvlc.dll");
System.load("C:\\Users\\sajad\\Documents\\Downloads\\Compressed\\JVLC\\libvlc.dll");
According to this tutorial:
You need to set LD_LIBRARY_PATH (on Linux/Unix) or PATH (Windows) include the directory where the libraries are.
You don't need the .dll suffix.

Categories