First off, I am pretty new to this. I am working on a desktop application which uses hibernate and an embedded h2 database. I am trying to create this project with 'modules' so that we may easily update code as needed. Right now I am trying to create a separate project witch contains our entity definitions, named-queries, and persistence.xml file. I created a JPA project to do this. My question is, how do I export this jar in order to be able to replace it in the future when changes are made? I create a runnable jar which points the the main of the core application, which I am not sure about, and it will run. My problem is when I create the lib folder next to the runnable jar of the core application, my custom 3rd party library containing our entity definition is not there? I am guessing this is because of how I pointed the library to the main of the core application, but as I said I have little experience with this so I am not sure. I cannot seem to find anything helpful out there but I know it must exist. If anyone knows what my issue is or where I should go to read about it, I would be most grateful.
It seems that the problem is with how you package your app? Be aware that a JAR file cannot include other JAR files (at least they are not included in the classpath). Only WAR and EAR may contain another JAR files. If you want to include a JAR nested in another JAR, you must repackage them and merge into a big JAR file, not only put one into another.
Related
right now I am struggling with very specific problem. I am building modular app where most of the modules are strictly separated so there is no coupling. However some modules can use the same large parts of code so I thought of to make them as another modules and handle it via some dependency system to avoid building modules with the same parts of code.
My question is, as the modules are optional, is there a way how to build one module (jar file) dependent on another module? In other words, is it possible to build separate but coupled jar files I know they will be always together in specified file paths?
I would appreciate any suggestions, thanks.
In your first JAR, at the start in main method, check if the another JAR file is exists.
If it does not exist throw an error message and don't proceed till you find the second JAR file.
I have a java main project and a java library project which is added as a library in the main.
The library has some .jars with essential content for both (the main and the library). I thought the main project could access these jars, but apparently not. Am I correct?
So... I thought I would have to dupplicate the .jars from the library and put them also in the main project. But this is quite awful. So I found the "Adding external .jar" from eclipse which let me add this .jar from the library, but my question is: "What will happen when I build an unique .jar? Will the .jar be added to the library and also to the main dupplicating itself?"
Also, can I import as a library an open project wihout having to compile it into a jar and be able to edit the project in real time?
Thanks
When you build a unique jar for your own code, it will not include the classes from the other jar. If you want other people to run your program, they will either need that jar themselves, or you will have to distribute the additional jar with your own jar (assuming that's allowed).
You could unpack the jar on which you are dependent and put it in your own jar, but this is unusual and not recommended. If the people who wrote that code corrected bugs and distributed a new jar, people who got their new jar would get the fixes and improvements, but not people who were using your packed version unless you distributed a new one.
I have no idea what your last question means...
You can certainly have one project depend on another, which sounds like what you mean by "can I import as a library an open project wihout having to compile it into a jar and be able to edit the project in real time?".
To share the jars from the library project, go to the "Order and Export" tab in the Build Path dialog, and tick the jars you want to make available.
Well, maybe it's convoluted, anyway...
I'm not very experienced with java, though I've dabbled in creating some Minecraft mods and Android apps.
My question is: I have a .jar that contains code that I don't have a lot of control on (I don't have the source code though I do have some infos about the classes in it), and when this jar is run it itself load some code from other external libraries and classes, and consolidate everything.
My question is then, how do I, without touching/modifying the jar, make a java program that runs the jar, let it do its consolidating from other jars and external classes, then get the results (a few objects) of that consolidating into my own java program ? Is that even permitted in the java security model ?
I've heard of URLclassloader that I think is to load load classes from a jar. It can't seem to make it work, and I'm not even sure that would work ? I know roughly which classes are entry point in the jar in order to run it and make it load the external libraries. But I always run into exceptions left and right.
First you should make sure all 3rd partyd dependencies (e.g. libraries that your jar needs) are ~visible~ to your application (e.g. reside on classpath and/or -Djava.library.path). Next, you should just instantiate classes/call methods from your jar file normally and operate with returned objects as its been locally created by your Java application. jar file is just an external library that becomes the part of your application upon loading.
I have a big application as an enterprise scale, there are lots of developers working on it; it is a Java Spring app, we use Eclipse as development environment. We have lots of libs, which means jar files in the lib directory. I can't touch to these jar files so easily, because whenever I change some jars, application may get failed/unstable because of little changes of the jars' new versions. On the other hand, as this application is a big and old project, I can't know what jar is really needed and which line, what function or class uses/needs this jar exactly.
So I have a problem now: As an example naming a-lib-1.0.jar is old jar and a-lib-2.0.jar is new version; I need to use a-lib-2.0.jar but a-lib-1.0.jar is already in lib directory. How can I know that a-lib-1.0.jar is needed or not without testing by deleting it?
And how can I find the line/method which uses this jar?
Please check Tattletale from JBoss.
What you need is a Dependency Walker.
Try this:
http://www.dependency-analyzer.org/
http://depfind.sourceforge.net/
I have the following problem:
I am writing an application that uses some of the JARs from the Netbeans Platform. To be exact, I am only using the Netbeans Visual Library for creating some graphs. This can be done without using the Netbeans Platform by extracting 3 JARs from the platform. This is all working well, except for 1 problem.
Some Background
I am using the Java Simple Plugin Framework (JSPF) to handle my plugin management. So I have an application that basically consists of a skeleton framework, and then depending on which plugin JARs it finds, it can do various things, one of which is drawing graphs. The JAR plugin for this functionality has all it's dependant libraries inside. This is done by exporting the JAR as an artifact in IntelliJ, which will unJAR all the dependant libraries and reJAR them inside yours (so everything you need is there).
The Problem
What seems to be happening though, is that when it tries to start use the classes from the embedded libraries, it works fine, but when it needs resources (.png specifically in my case), it complains that it cannot find it.
My Thoughts
The only thing I can think of why it is not working, is that it could be since the plugin JAR is not in a classpath. Could this be it?
Is there anyway to specify a classpath directory in the MANIFEST maybe? Otherwise must I create my own ClassLoader and manually load all the JARs in the plugins directory?
Thank you!
UPDATE:
I have subsequently pinpointed that it is indeed a problem with the classpath. If I place my compound library on the classpath, everything works perfectly. The problem I experience now though is:
If I copy the library to /Library/Java/Home/lib/ext/ it works fine. If I execute the application with java -cp "/path/to/plugins/myLib.jar" -jar Application.jar it does not work.
How can I load all the jars in the plugins directory into my application so the resources inside them can be used?
Thanks again!
So I have finally figured out what was happening. When creating a executable jar, the MANIFEST.MF file overrides any classpath you specify in the command-line, which basically renders it useless if you want to specify external jars. This seems to be a general problem that has been logged since Java 1.3 already.
My simple solution is to simply not create a executable jar, and then launch the application with a script:
java -cp App.jar:plugins/* my.package.structure.App
which works perfectly.
The default classloader's do not load classes in nested jars. You'll need to write your own classloader to get the classes in the nested jars.
You can check out this jspf article...
"I forgot: Adding dependencies as JARs inside JARs is not possible, because it would not work in all scenarios (e.g., applets); IIRC also tools like Eclipse would have problems if you used classes with unresolved (read: runtime-resolved-dependencies). To my knowledge there is no established way yet to gracefully handle nested JARs in all circumstances."
http://code.google.com/p/jspf/wiki/UsageGuide