Add jar into running environment - java

I've got a question on how to add a jar file with code.
The situation is that I want to allow the customers to choose themselves, which database should be connected. Therefore, I'd like to give them the opportunity to load a custoom *.jar into the running software (similar to Add external library in eclipse).
Is there a way how I can manage that? I was trying kind of
import System.getProperty("java.io.tmpdir") + "\\dbdriver.jar";
java.io.tmpdir\dbdriver.jar would be the file, where custom jar-library-imports will be stored by my code. But eclipse didn't seem to like it.
Do you have any idea?

You'll need to read up on Classloaders and Reflection in order to get an understanding of how this works.

This is a problem of loading jars at runtime.
Please take a look at the following link which is pretty similar to what you're looking for.
Loading jars at runtime

If this is what you are trying to accomplish in a .java file it is not correct, you cannot reference a jar file directly in a java file, you can just import single classes or a group of classes using wildcard '*'.
The best approach, to my knowledge, here would be to install an ORM library and then decide with the client what his choice for "more than one" RDBMS would be.

Related

Retrieving and sorting list of files at runtime from Jar

I'm fairly new to Java (and stackoverflow for that matter) so I'll try my best to be on-point. I'm using Eclipse Juno and JavaSE 1.7.
I would like to create a data structure which allows me to dynamically sort and retrieve the files I'll need at runtime. These files will be scripts which are processed depending on "where" they were found, or what "type" they belong to. For example, a script relevant to processing commands from the command line would be a "command" script.
I've learned that "directories" in a Jar are more-or-less meaningless, so a clever usage of those proved useless. Although, there are some hacks to make it work, I'd like a more robust solution.
(I have seen the following threads questions and answers:
How do I list the files inside a JAR file? and
Listing the files in a directory of the current JAR file)
After some thought, I've decided I need to rethink how I'll retrieve these files and this is where I'm at a loss considering my underwhelming knowledge of Java still. I find myself hopping around the internet aimlessly looking for answers, which is when I decided I should ask the question directly.
My questions is this: What is the best approach to creating a meaningful file structure which allows me to retrieve (script) files relevant to how I want to process them at runtime?
There are many answers to this problem. The real question is how is this information going to be used by the target audience of this program? That is, are you aiming at developers who can whip up some code? Advanced users that just need to add some configuration? Or the general populace with no significant computer experience?
For the first, you could create your own annotations, and find all the classes in the class path that have the annotation. This would allow developers to add classes to your system just by adding some jars to the class path.
For the second, maybe you could have a plugin directory, and iterate over the files there to find the defined extensions? Or maybe have a configuration file that lists the plugins you should use?
Finally, for a general user, you should make it simple. Have the GUI allow the user to specify additional capabilities by location? or website? Like Firefox addins, maybe?

Hibernate: is it possible to reduce file size of jar?

I am working on a desktop application, I use Hibernate and HSQLDB. When I make my application a runnable jar file, it has a bigger fize size than I think. I see that the biggest part is from Hibernate and its dependencies. I am not sure if I need all of the Hibernate features. Is there a way to get rid of the parts of Hibernate and its dependency libraries which I don't use?
Under the /lib/ folder in Hibernate zip you will see a folder called /required/. For very basic Hibernate apps thats all you will need though you may need additional JARs for things such as JPA. I would start by only including the JARs in the lib/required/ directory, see if your project works, and if it doesn't add what you need to get your project working again.
perhaps you could use a tool to analyse your classes and dependencies (for e.g. http://www.dependency-analyzer.org/). Here is another post about it: How do I find out what jar files are actually used when compiling a java project.
the other way is to remove some jars (or even single class files) and try whether your application is still working or not. but i think this is not a very good way...
I can't think of a better tool for this than ProGuard.
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.

Using serviceloader on android

I am very new to java and android development and to learn I am trying to start with an application to gather statistics and information like munin does. I am trying to be able to load "plugins" in my application. These plugins are already in the application but I don't want to have to invoke them all separately, but be able to iterate over them. I was trying to use serviceloader but could never get the META-INF/services into my apk. So I am wondering if it is possible to use serviceloader on android
Thanks
EDIT: I am asking about java.util.ServiceLoader, I think it should, but I can't figure out how to get my services folder into META-INF on the apk
There is an open bug report against this issue. See https://code.google.com/p/android/issues/detail?id=59658
The META-INF folder is deliberately excluded from the APK by ApkBuilder; the only comment in ApkBuilder.java is "we need to exclude some other folder (like /META-INF)" but there is no other explanation.
Even after adding META-INF with ant, you will still get in trouble if you want to use Proguard, which refuses to replace the content of META-INF/services/* files or rename them (that's another story, the author wants to keep Proguard agnostic).
However, people using maven may want to check https://github.com/pa314159/maven-android-plugin (the branch named "modified"), that tries to solve both issues. It is a fork from the original "android-maven-plugin" I modified one month ago for my own Android projects.
It also provides a patch for Proguard-4.7
Hope this helps, any feedback is welcome.
I've figured out a solution that may work for some situations. Instead of ServiceLoader, I'm using the org.openide.util.Lookup class / library that comes with NetBeans - it is a superset of ServiceLoader. It does not require NetBeans itself and seems to work ok with Eclipse. It is necessary to replace whatever ServiceLoader functionality you are using in your application with Lookup equivalents, and add the org-openide-util-lookup library. Then, you can just do something like this:
Lookup lookup = new ProxyLookup(Lookup.getDefault(),
Lookups.metaInfServices(myClass.getClassLoader(), "services/"));
And move your ServiceLoader files from META-INF/services/ to services/.
Note that, because of the ProxyLookup, this will continue to work on standard Java environments unchanged (i.e., in those cases it will continue to look in META-INF/services).
Here is a link to the documentation for the library: http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/Lookups.html
UPDATE
After working with this for a couple of days, it seems to function well - I move between environments (standard Java and Android) and it works properly in each location. The primary downside is having to manually copy the files to the /services directory.
It is possible. You may want to check http://developer.android.com/reference/java/util/ServiceLoader.html
ServiceLoader is stuff from the Java language that is not really relevant on Android. I recommend not using it. If you just want to find a list of classes within your .apk to load, there are all kinds of ways to do this -- put in XMl file in res/xml that lists them, use reflection, annotations, etc.

Search Java jar (ear,war,...) files for class / method

I am looking for a tool where I can search several jar, war, ... files for a certain class and method (even better Signature).
So far I have found tools such as JavaClassFinder where I can search for java files and classes. What I would need is to search for methods / signatures as well... I am looking for an existing solution, I do not want to write my own tool.
Example: MyMethod should find all MyMethod methods
Any ideas, suggestions?
---- Edit 2014/March -----
See https://stackoverflow.com/a/23112227/356726
I don't know about a standalone tool, but if you create a simple Java project in Eclipse and add the jar, war, etc files to that, you can use the "Java Search" search dialog in Eclipse.
This does exactly what your want and among others has an option to search for methods, and even allows you to filter those on references or declarations.
Alternatively you do have to build something yourself, although you could always use an existing tool as a start, like JavaClassFinder you mentioned or JBoss Tattletale.
I just wrote a small (OpenSource) text search utility for JAVA archive files. It can cover your requirement to some extend.
EAR Search can be downloaded from http://sourceforge.net/projects/earsearch
By chance another comitter in another question has left a link to http://code.google.com/p/reflections/ (honour to this link). THis would help if you need to do this at runtime.

Can you access items inside a jar using File

I have some files inside a jar which I would like to access in Java using a File object rather than as a stream. Is it possible to do this?
Look at JarFile.
java.io.File is an abstraction from os specific handling of files. If you use java.io.File in your code, the code should run on all Java platforms.
The Jar is not a os file system. So it makes no sense to apply java.io.Files from the Java core classes.
I don't want to say it is not possible. Maybe it has sense for certain application and there is a library for that kind of abstraction.
You can also access it as a URL with a "jar:" prefix, but that's not a File object either, so I guess that doesn't meet the restriction.
Why do you have to access it as a File? This seems like asking, "Is there any way I can add two numbers without using the plus operator?" Maybe you can, but why do you not want to do it the easy way?

Categories

Resources