Creating a Jar File Containing Another Jar File - java

I've created an Android project that does some very simple network stuff. This project is standalone, and I've successfully exported it as a jar file and imported it into other projects I've worked on. When exporting this project, I am ONLY exporting the class files that are used for the project. (No system files: ie. Manifest, etc)
I want to leave this project as standalone since it is used in other projects and will be used more in the future.
However, we have other projects that need the features of the network jar and some other features. What I would like to do is create a new project, import the network jar file. Create wrappers for all the functions in the network jar file, then add more functionality.
Afterwards I want to package this project up as a jar file, so that I can use it solely for projects that require that extra functionality.
Having this project as a jar is important, because this component might be given to other people to use, and easy/simple integration is required.
This seamed like it should be pretty straight forward to me. But I am getting noClassDef errors and Verify errors.
I even stripped the project down to a single function that returns a static String from the original network jar and passed that through and still got a "could not find method xxxx referenced from method xxxx" log and then a unable to resolve static method warning, then a No ClassDefFoundError. Here's the logcat.
What is the proper way to do this?
Thanks!

Jar files that want to use embedded jars must define a custom class loader, like JarClassLoader.
If you want to use a single jar file you also have options like OneJar, JarJar, and ProGuard.

Related

Accessing files in a project that is on the build path of another

I'm working on a framework-type project in Eclipse, that has a res folder attached with, as you guessed Resources inside of it. At the moment, they are crucial text-files that are to be read.
I also have a main test rig that is apart of it's own Eclipse project, as I plan to expand it in the future. This test rig has included the framework project on it's Build Path, so I can access classes from the framework as they are added.
But this is where my problems lie: If I want to import a class, that works fine. If I want to access a file through an InputStream, or Reader, it breaks. I know this is because the files included from the secondary project aren't placed inside the test rigs bin folder, rather they reside in their own bin in the other project.
Is there a way I can get my main project to access resources from the other without having to do ../../ trickery in my file paths? I plan on making this a real thing and don't want file paths doing things like that.
In eclipse do this,
Build Path ---> Configure Path ----> Click Project Tab ---> Add the project.
Also, you can package the other project in a jar and import it as an external jar.

Export to JAR with external JavaPOS Files

I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.
The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.

Use JARs of linked projects instead of .class

I am building a desktop application with NetBeans 8.0.2. For my application, I have to manage 3 differents projects : The main project, and two "tool" projects that are linked to the main.
When I run the main project, it will check the JARs present in his classpath in order to retrieve the Manifest files and do some work with.
In order to have my application run correctly, it has to see the two linked projects' JARs but it doesn't, because NetBeans deals with the compiled classes of the project instead of the JAR (for debugging purposes I presume).
I found nothing about it on the Oracle documentation, and the only thing looking a bit like what I search is to create a big-fat-JAR by using another component.
Is there a way to tell NetBeans to "compile the linked projects and use the JARs instead of the .class" files ? Thanks in advance
EDIT : Here is an example when I add the project with "Add Project .." option
/C:/Users/xxxxx/Documents/GuiceProjectsRD/xxxReaderRef/build/classes/
And here is an example when I add the JAR
/C:/Users/xxxxx/Documents/JavaLib/xxxReaderRef.jar
When I add the JAR, I have the ".jar" extension which helps me identify a JAR and then look into it for a Manifest. When I add the Project, there is no path to the JAR but only to the compiled classes, and I can't work with that.
I would not depend on the Manifests in Jars since you then get this kind of issues.
Have a look at the Typesafe Config library. It's a small 100% pure Java library to work with Json/Hokon configuration.
Instead of relying on a Manifest, create a 'reference.conf' in each tool project. In your application, create an 'application.conf' (if needed). Load the config via 'ConfigFactory.load()'. It will automatically search all available reference.conf's, and application.conf, on the classpath, whether in a jar or not, and merge those configs into a single configuration.
I use this approach in project to be able to plugin extension. Have for example in tool A a configuration like
tool.A.class = 'my-tool-A.class'
or used nested structures like
tool {
A {
class = 'my-tool-A.class'
}
}
Do something similar voor tool B.
Then in your application, from the Config, you can get a list of 'tool' configs and detect the available tools like that.

Java trying to find 3rd party class in my package even though that class is in an included JAR

I'm new to Java and NetBeans and I'm having a very hard time getting a simple project started.
I'm trying to include .jar files I need to work on a NetBeans plugin. I can successfully add the .jar files to my project using a variety of attempted methods.
I added .jar files by the project properties and added the 'wrapped jar' files to the project
I added the .jar files to the 'Libraries' item in the Projects explorer tree
Both methods appear to work in the IDE. They allow the desired classes to be accessed in the IDE and no syntax or access errors (etc) are detected. However, when I build and run I get ten pages of errors such as NullPointerException and this doozy:
java.lang.ClassNotFoundException: Will not load class org.netbeans.modules.openide.nodes.NodesRegistrationSupport arbitrarily from one of ModuleCL#2afef4c1[org.openide.nodes] and ModuleCL#2debe24[com.myproject.simplelauncherbutton] starting from SystemClassLoader[316 modules]
com.myproject.simplelauncherbutton is my own package. Why would NetBeans even be looking in my package for this class? And even if it is looking there, how can it be finding the class there, to be confused? I just want to make a NetBeans plugin using a .jar file for support. How can I get this working?!
Solved with help from a coworker. The problem was the way in which I was including packages in my project. I was trying to include packages in my project that did not seem to be available. For example, I needed to use org.openide.nodes, so at the top of my class file I wrote:
import org.openide.nodes;
NetBeans would respond saying it couldn't find this package. When I found a wrapped JAR package containing org.openide.nodes and included that in my project, it generated a slew of errors too long to list here.
However, when I add the module by its English name "Nodes API" in the project properties, everything works fine. I wish there were some documentation or instructions I had been able to find to save me hours of stressing about why I couldn't get NetBeans to recognize the various versions of org.openide.* I was trying to use.

GWT Client External library

I'm working on a GWT project to which I've built a .jar file to handle some processes, it is however reusable in other projects and therefore I want to keep it as a seperate project. But when I want to run the code in GWT (adding the jar to the build-path) I cannot seem to get it right.
I have added, to the root of my external project a "xxx.gwt.xml" file ("xxx" is the name of the file and gwt.xml the extension) and then I've set the source-path (the -tag to "com.xxx" which is the root of all the packages I access in the GWT program. Then I've made it a .jar, added it to the buildpath of the GWT - project and then also added the source-code to the jar, still it finds the "xxx.gwt.xml" file and then it complains about the import of the "com.xxx.yyy.YYY" and that it cannot resolve the type of "YYY".
Also notice that I'm not calling any methods which contains any libs that aren't allowed in GWT and I'm not getting the warning "non-canonical package" which you usually get when GWT doesn't/can't resolve/whatever something is wrong your package in the -tag.
Thanks in advance!
You also have to define a module in your .jar and then inherit it in your external project.
See http://www.vogella.com/articles/GWT/article.html#modules_use

Categories