I've Goog'd but I can't find any third party library that will let you create a jar file programmatically. I amazed that even Apache commons-io doesn't seem to have such functionality.
I'd rather not implement it myself as the API is rather low level. Are there any 3rd party libraries out there that will do the job?
Edit: I meant programmatically in Java. I don't want to drop out to the shell and I don't want to work with java.io.*.
I'm not sure what you mean by "creating a jar file" but ant has a jar task which will create a jar file for you
http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
You don't need a third party library. It's all built into Java (the jar command just uses those classes)
http://download.oracle.com/javase/6/docs/api/java/util/jar/package-summary.html
Edit:
It will require you to still work with java.io.* as that is the only way to create files...
I'd suggest you look at JBOSS Arquillian. It provides a fluent interface to create jar, war, ear etc. See sample below
Archives.create("test.jar", JavaArchive.class)
.addClasses(
GreetingManager.class,
GreetingManagerBean.class);
Related
this may sound like a noob question, but it is a big problem for me. I have a file called yandex-translator-java-api-master.zip, and I tried adding that as an external JAR to my eclipse project, but the code wouldn't work; It wouldn't let me import.
This error comes up when i try to import "Translate"
Translate cannot be resolved
6 quick fixes available:
Create class, create constant, fix proj. setup...etc, etc.
How do i use this??? Where is the .jar i am supposed to import?
zip archive is not the same thing as jar. First, extract jar from zip archive and then add it as dependency. Then you'll be able to import.
UPD:
looking closely, I suppose you downloaded that archive from GitHub (or another VCS) and it's just sources of a library. You can not add it as a dependency, but you can, for example, just paste this code in your project's /src/main/. This is an easy solution. If you want to make things the right way, you can
a) Search for a compiled library
b) Create a module from downloaded sources and add it as dependency
Download this file:
And established it as a library.
Instruction is in this answer.
I have a jar file that cannot be modified, but I want to use a different .class file in place of one of the members of the jar. How can I tell Java to use the external .class file when the code within the jar attempts to load it?
You could compile another jar file with replacement classes with exactly the same name and put it ahead of the jar file in the class path. For example, this is what the various slf4j bridge jars do to replace calls to log4j or Jakarta Commons Logging in library code with cognate slf4j code; one need not maintain two sets of logging systems and configurations that way.
If you want to override a java... class, you can use some of the command line options to change the boot class path. Look at the -Xbootclasspath options in http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html. Heed the warnings.
There is also the lib/endorsed directory if you need to upgrade a third-party jar that Sun uses. Oracle uses other organizations' XML and CORBA libraries; if they release a new version and you need to adopt it, you can.
You can use AspectJ to instrument the code and possibly replace it. An around advice can call the original code if it wants.
You could see if you really need to replace the original code. Some systems provide customization hooks.
You need to make sure the external .class file is loaded first. If a class is already loaded by the class loader then it will not be reloaded. If you are using an application server, then there are ways to configure the preferences for loading classes for class loader. But if you are using a standalone application then you may need to extend the class loader to load the files in the order you want to.
I have a runnable java jar file that I need somehow to run (pass params, fetch output) from another java class I'm working on. How do I do that? Do I import it as a package somehow, call it on runtime? Can I invoke "main" method from it or do I just run it with "exec"? Thanks for your answers.
Simply add it to your CLASSPATH and call either the main() method or any other public method which it provides (and which is documented). There is no difference between "normal" and "runnable" jar files, besides an entry in the manifest.
One subtle detail you might need to consider is that runnable jar files are usually self-contained - that is, they contain all required classes, including classes from third party libraries. If you are using the same third party libraries in your project, make sure that there are no conflicts, e.g. by removing the separate third party jar files from your project.
See Lesson: Packaging Programs in JAR Files for more information.
Import it in your code if you need something, that you can not achieve from command line exec solution. Note, you can get problems, if you trying to use some methods from jar file itself, and author changes it later.
Consider Bridge pattern, to put some abstraction layer between jar file and your code, If you not sure, that you use public API.
If jar file is library with stable API, you can be more confident, while using it in your project.
Besides, importing jar file and use methods from it is faster, than parsing process output.
u can import it to your project and then call the publicly exposed methods in that jar from within your application.
I've developed a utility library that will be used in many of our enterprise Java applications. This library has numerous additional dependencies that also need to be on the classpath. I'd like to avoid forcing our developers to add a zillion entries to their MANIFEST.MF files, and let them instead just include my library. Is there any way that my library's MANIFEST.MF file can reference its dependencies and have them picked up by the enterprise applications that will be using my library?
I've tried referencing them in my library's MANIFEST.MF file using the full path to the dependencies on the filesystem. That didn't work. I end up with ClassNotFoundException errors for all of my dependencies. Is there something else I should be trying?
When you create a web application, you'd normally put it in a WAR file. The idea is that you bundle the required dependencies in that WAR file, by adding the jars to the /WEB-INF/lib folder inside the WAR. Web containers (like in a Java EE application server) know of this structure and will include those jars on the classpath.
If your library has additional dependencies, just tell the users about it and either redistribute them with your library if the license allows it, or tell them where to obtain them. When using a decent tool for creating a web app like an IDE, Ant with Ivy, or Maven (or a combination of these), then handling and bundling dependencies should be no problem.
Alternatively, this works so long as you stick to the format very carefully, i.e. stick to exactly two spaces before each "file:" etc:
Manifest-Version: 1.0
Main-Class: package.TestClass
Class-Path: file:/D:/WebServer/Tomcat/shared/lib/BlueCove.jar
file:/D:/WebServer/Tomcat/shared/lib/classes12.jar
file:/D:/WebServer/Tomcat/shared/lib/comm.jar
file:/D:/WebServer/Tomcat/shared/lib/FTP.jar
file:/D:/WebServer/Tomcat/shared/lib/FTP2.jar
file:/D:/WebServer/Tomcat/shared/lib/iText.jar
file:/D:/WebServer/Tomcat/shared/lib/j2ee.jar
file:/D:/WebServer/Tomcat/shared/lib/jmxremote.jar
file:/D:/WebServer/Tomcat/shared/lib/jmxri.jar
file:/D:/WebServer/Tomcat/shared/lib/jmxtools.jar
file:/D:/WebServer/Tomcat/shared/lib/jpos15.jar
file:/D:/WebServer/Tomcat/shared/lib/js.jar
file:/D:/WebServer/Tomcat/shared/lib/mail.jar
...
file:/C:/WebServer/Tomcat/shared/lib/soap.jar
file:/C:/WebServer/Tomcat/shared/lib/sqljdbc.jar
file:/C:/WebServer/Tomcat/shared/lib/tools.jar
I've done this with a number of tools. It is a truly horrible hack but seems to work reliably.
Give them a special manifest to use. Something like:
Manifest-Version: 1.0
Main-Class: com.xxx.yyy.zzz.YourSpecialClassThatHacksTheClassPath
Real-Main-Class: com.ppp.qqq.TheirMainClass
In your special class, screw around with the classpath (not easy), read the manifest "Real-Main-Class" entry (a bit easier) and launch their main from that (not really difficult at all).
Obviously this will not work with a .war file.
Even I had the same problem. As mentioned above, the solution was to have exact two space after file:/ and one space after .jar file and at the end, press enter key.
I know this is not a neat solution, but it works. enjoy.
I am working on a GUI application and would rather distribute just one jar as opposed to multiple ones.
Can you control this with the manifest.
Another option is to use a custom class loader such as this one:
http://one-jar.sourceforge.net/
Merge your jars to one jar. See this thread.
You need to be careful when doing this. If the jars you are merging have manifest files with critical information - these can get lost, only the last file will get merged.
E.g. If you merge JavaMail - the manifest file is important. If you lose it - bad things csn happen.
The safest thing to do is to look at each jar file and check the manifest file.
Have a look here. Use Netbeans 6.7.1 to combine multiple jars into 1 jar
Create a Shaded jar which combines multiple jars into a single jar. Maven supports this functionality out of the box.
https://maven.apache.org/plugins/maven-shade-plugin/