I have a little Java applet game where you can choose between some themes. It works very well but the downloading time of the huge .jar is not acceptably. Now I want to split the .jar into single .jars, a default one and one for every theme. Now there is just one question: (How) can I read a .jar file from a Java applet which is also a .jar?
Take a look at the URLClassLoader. You can give the URL to the theme.jar as a parameter and use the getResource* methods to access the files inside.
Another approach would be to manually download the JAR and open it with the java.util.jar classes, but I would go with the first approach.
Deploy the applet using Java Web
Start. From Java 1.2 this could be
done to get a 'free floating' applet
(outside a web page), but since
1.6.0_10+, it can also be done for embedded applets.
Put each theme in
a separate Jar and in the JNLP
(launch file) & mark them as 'lazy'
download.
Notate which package is
contained in which Jar (also in the
JNLP file) so the JWS client knows
which Jar to download for each
theme. (a)
Everything else will work 'like magic', and the JWS client will show a progress bar when downloading the lazy Jars.
(a) For this to work properly, each theme needs to be in a separate package, as well as a separate Jar.
Related
I have recently completed a java program and Im now looking into options for deploying it. I was introduced today to Java Web Start. My problem is the following: my program contains the main executable jar, a lib folder that contains the libraries which my program uses, and a couple of other folders which contain files that my program reads from and writes to. Is it possible to use Java Web Start with such a structure? All the examples I have seen on the web were simple programs made up of just one jar. Thanks.
The extra libraries are no problem. Just add a reference to each one in the JNLP resources section.
The 'loose files' are, slightly, in that loose resources cannot be referenced in a JNLP and archives are not writable. But even that is fixable. See the accepted answer to How can an app use files inside the JAR for read and write?
I have just written a java application to talk to a blood machine. And it posts its data to a web server using OAuth security and XML, and the application works great.
But now I need to distribute it to customers who have these machines. This program is in a .jar file and its also requires other .jar files which are in my project/dist folder.
During the execution it uses little .png files which are displayed as icons on the screen, that the user can click etc.
I want to some how wrap all of this together (including the images) and distribute them either by an installer or i can use a web server.
What is the best way to accomplish this, i tried to copy the .jar and the dist folder (as per the instructions that came out of netbeans) but when I run java -jar myfile.jar it prints out the text i had from the IOException because i have not included the .png files.
Code for the PNG files:
Im using this:
try
{
databaseimage = ImageIO.read(new File("image/database.png"));
databaseDisconnectedImage = ImageIO.read(new File("image/database_delete.png"));
}
catch (IOException ioe)
{
System.out.println("Problem Creating Systray: "+ioe.getMessage()+" Current Working Directory is: "+System.getProperty("user.dir"));
}
You have a number of alternatives including the following:
Build an installer that installs the relevant files on the user's machine. You can either write it from scratch or use a commercial or open-source tool to create the installer. (Installing on multiple operating system types gets complicated ...)
Restructure your application so that all resources (e.g. images) are fetched via the classpath rather than directly from the file system. Then create an executable "Uber-JAR", by exploding all of the dependent JARs and resources, assembling the files into one tree, and JAR-ing the tree. (The JAR is made executable by adding a couple of entries to the manifest.)
Create a JNLP launcher for the application. This only works if the end-user is going to be able to access the website that hosts the application files, but it neatly solves the problems of updating the application, and ensuring that the user is using an up-to-date JVM.
Which is best? Each alternative has pros and cons, and you will need to work out which is best for you.
Heavy-hitting solution is to use Maven and create an executable jar, a.k.a. an assembly, a.k.a. a jar-with-dependencies. A jar can contain both its dependencies and the meta-data to run it properly.
java -jar Client-1.0-SNAPSHOT-jar-with-dependencies.jar
Would work. I assume there's a similar solution using Ant, which is another tool to build Java programs.
More manually: Just think for a second - whatever command including the classpath you use to run your program, make sure to save that into a script. All files that it needs bundle it up into a .zip and send that over, with the instructions to unzip and cd into it. Ultimately you're just running a bunch of files, you just need to send them over and preserve directory structure so your script will work.
I need to load an .xml file in META-INF, it works when the application isn't sandboxed, but in Java Web Start a different classloader (which is more restricted) seems to be used, so the file found in myproj.jar/META-INF/myfile.xml isn't loaded. It however works if I put the file in the current directory of the loading class (I put it under com/blabla/myproj/whatever/META-INF/myfile.xml).
I couldn't find any classpath settings within the .jnlp file, but perhaps this can be done with a manifest? I don't know how they work, so if that's the solution please supply an example.
Stuff in META-INF should not be readable by code in your jar file, since that code should be agnostic to the fact that it's packaged in a JAR. Instead, since it is meta information (meta-inf) about the Jar itself, only the application that loads the jar file should access it.
I have a Java application installed. The jar is bundled into an .exe file using Launch4J. Now I want to create a patch for this application.
If I create another jar containing only updated files, how can I refer it in the original code?
I have java application installed. ..Now I want to create a patch for this application.
This is one of the strengths of the Java Web Start launch technology that comes with the J2SE. Simply update the Jar on the server, and the next time the app. launches, it will be updated.
The update can be honed for your use-case, configured to be done lazily or eagerly, before or after launch, or even programatically controlled using the JNLP API's DownloadService.
..And the jar is bundlled into an .exe file ..
'Unfortunately', JWS works on Windows, ..and Mac., and *nix - so you may have to expand your horizons.
BTW - I have no idea how to do the same with Launch4J, but then, that is really the wrong question. I aim to provide an answer to the right question, which is "How do I deploy & update a Java rich client?". ;)
I've never worked with Launch4J, however I think you should try to affect the classpath. JRE always loads the classes from the classpath. From this point of view, jars have no added value and just serve as a containers for your *.class files and resources.
Now, if you succeed to configure your tool to do something like:
classpath = C:\Temp\my_patch_path;$your_current_classpath
then its enough to put your changed files into C:\Temp\my_patch_path (of course preserving the package structure). JRE will load your classes first in this case.
Hope, this helps
Mark
It is might not be possible to do this without changing the contents of the exe.
I need some help figuring out how to make some changes to some .jsp pages that are contained inside of a .war package.
I am using jbilling opensource billing software and need to modify some elements/display and want to do it directly in the .jsp, so I unpacked the .war file and made a change, then packaged it back up and put it inside the webapps folder and restarted tomcat. But I noticed the filesize from the .war that I packaged was smaller than the original .war and it should have been larger because I added stuff to it. Needless to say, tomcat didn't start up properly, or at least there were errors in the logs..but they didn't help me and jbilling didn't work right. I reverted back to the orignal .war and it worked fine.
Is it possible for me to unpackage a .war and simply make some html changes, then repackage it up without having to recompile the whole source code with the java classes? Did I use the wronge "packaging" tool to compile it? Is there another way to accomplish what I'm trying to?
I did this in a FreeBSD box with using the following commands:
unpackage-->sudo jar cf ../billing.war *
repackage-->sudo jar -xvf billing.war
Thanks for your help.
Moreover, .WAR and .JAR (AFAIK all it goes for all .*AR) are basically glorified zip files conforming to some structural requirements (manifests, web app descriptors, etc.). The easiest way to do the changes you want are to handle the .WAR file as if it was a plain-simple ZIP file. The choice of the tools is all yours (archiver, pkzip, etc.).
You can simply open war file using archiver utility (seems you are using ubuntu).
Open jsp file from archiver util it self , make changes , and Archiver util will ask that jsp file has been modified you want to update your war file say yes there.
Note: If you are going to do only view changes than go for this tricky way otherwise if you are willing to change source java files than building the war will be strongly recommended