Auto-Launch JNLP file 'programmatically' - java

How can I auto launch JNLP file "Programmatically".
I have been able to auto-download the JNLP file but then I have to click on the downloaded file to run it. I'm aware that I can 'open' it every time instead of 'saving' it and remembering this choice. But this is not what I want, I cannot tell client to open the file every time.
Can this be done programmatically?
I suspect that this(opening JNLP instead of saving, programmatically) can not be done, but I absolutely have no idea, any help would be appreciated.

Yes launching a JNLP file programmatically can be done, see below code is using Apache common io to copy streams (Line 3) but there are different ways to copy streams:
final File jnlp = File.createTempFile("temp", ".jnlp");
final URL url = new URL("http://your_jnlp_file_url");
IOUtils.copy(url.openStream(), new FileOutputStream(jnlp));
Desktop.getDesktop().open(jnlp);
I have based my code from below stack overflow question where one of the answers show how to call JNLP URL programmatically:
Combination of Launch4J and Java Web Start?

Related

Force JNLP file to update itself

This question has been asked before here in many different ways but without a satisfactory answer.
I have made some changes to my JNLP file, which is being used by hundreds of people. As I don't want them all to clear the cache on their local machine (not everyone has strong computer knowledge), I would like the JNLP file to get updated the next time they click on the link to launch the application.
How can I then force the client to download the JNLP file whenever that file changes?
Using JNLP attribute href or codebase is not an option since many of the users have bookmarked the link to the JNLP file.
Setting the Last-Modified header did solve the problem.

How can I determine the way to access certain third-party programs and resources from my Java app?

I'm trying to open a PDF file after I generate a report. I mean, the user logs in (it's a Swing-based app) and clicks to generate a report. Then, a PDF file is generated. I would like to launch the PDF reader at that moment. I could do something like exec("evince "+path_to_pdf_file). It's just for Ubuntu, Windows would be more difficult. I'm thinking I need to explore the registry.
How can I achieve this?
What you need is the method java.awt.Desktop#open
Launches the associated application to open the file.
If the specified file is a directory, the file manager of the current platform is launched to open it.

Java Desktop API not working with network paths? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Not possible to launch a file on a network using Java Desktop?
I am trying to use the Desktop API to launch the appropriate app for a file. So i am using this :
if (Desktop.isDesktopSupported())
Desktop.getDesktop().open(new File(path));
where "path" is a String pointing to the file.
Everything works fine until i try to launch a jpg that resides at a network location (for instance "\\MyNet\folder\image.jpg") when i get an IOException :
java.io.IOException: Failed to open
file:////MyNet/folder/image.jpg
Any one knows if there is a way to fix this?
I believe you need to specify the file location/name in standard URI format - which is close to the standard format except for servers. See the javadocs for the URI Class for more information.
At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax
[scheme:]scheme-specific-part[#fragment]
And a little later:
A hierarchical URI is subject to further parsing according to the syntax
[scheme:][//authority][path][?query][#fragment]
so the URI should look something like the following:
file://MyNet/folder/image.jpg
where "file://" is the protocol, "MyNet" is the server, and "/folder/image.jpg" is the directory location under the share.
Hope this helps a little.
file:////MyNet/folder/image.jpg is not a file path. It's an URL.
File f = new File("\\\\192.168.0.4\\mybookrw\\save\\command.txt");
Desktop.getDesktop().open(f);
Worked fine for me. The one caveat is that you have to be authenticated against the share already. If you paste the path into the run box and it prompts you for a username and password then its not going to work from an app.
Everyone so far has assumed that the file isn't being found.
However, looking at the Desktop open() function, an IOException is thrown
if the specified file has no associated
application or the associated
application fails to be launched
Now, having said that, what happens if you open a jpg on your local machine? Also, what happens if you try manually launching the jpg through the network?
Edit: Actually, the problem may be that the default program set to open jpg files doesn't understand file:// uris. Sticking with UNC paths might be a better choice.

input/output with Java Web Start

I'm working on converting a very simple java desktop application to run in java web start and I'm having all kinds of trouble with the input/output files. Most specifically I can't seem to find any information on how to handle i/o in a web start application. I tried placing the input files in the same folder on my web server as the jar and jnlp file, but it doesn't read it.
I've got one input file that I want to keep on the web server and read into the application from there.
I've got a second file that I want the application to generate on the client machine the first time it's run, and read in from there every time thereafter.
If anyone knows what considerations I need to take for i/o in java web start or can point me towards a resource that explains it I would appreciate it.
You can find out where you were downloaded from with BasicService and then use HTTP to transfer the file. You could also just add them to a jar.
You can store a limited amount of information (I think it currently defaults to 128K/muffin) with PeristenceService.
http://java.sun.com/javase/6/docs/jre/api/javaws/jnlp/
You don't have many choices.
You
could read the file from http new URL(address).openStream()
You could embed the file in the jar
which I believe you don't want to,
and then use getResourceAsStream()
You usually store files on the user hard drive using a hidden folder
public final class ApplicationConstants{
final static String HOMEDIR_STRING = System.getProperty("user.home");
final static File HOMEDIR = new File(HOMEDIR_STRING);
final static File CONFIG_DIR = new File(HOMEDIR, ".com.mycompany.myapp");
}
///
if(!ApplicationConstants.CONFIG_DIR.exists()) ApplicationConstants.CONFIG_DIR.mkdirs();
File outputFile = new File(ApplicationConstants.CONFIG_DIR, "my.xx"));

Having a lot of trouble deploying a java applet

I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.

Categories

Resources