Java opening files - works from Eclipse, doesn't work in runnable JAR/exe - java

I have some java code that I'm using to open a file:
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + output.getAbsolutePath());
It works just fine when I execute it from Eclipse, however, when I export the project to a runnable JAR, the functionality stops working. I've tried various different options to open the file (Desktop.open, ProcessBuilder etc.) all with the same results (Eclipse OK, runnable JAR not).
This exact code previously worked when I exported it in the past (1.5 years ago). I believe that was with Java 1.6 and now we're on Java 1.7. Not sure if that's the reason though and I don't have the option to compile using previous versions of Java. Any ideas?

thanks for attempting to help. I figured out the answer which is quite simply that I'm an idiot :) Opening the file wasn't the problem, the file wasn't being written in the first place, and the reason it wasn't being written was quite simply because the code was set to write it into a nested folder structure which had not yet been created. I didn't even think of this because I assumed that any necessary folders specified in the output string would be created automatically. Doh!

Related

Trouble executing a jar file

I am trying to run a executable jar file I made in Intellij on JDK 11.0.2 that contains a GUI system. I have java 8 and Java SDK 11.0.2 installed. Whenever I double click the jar file I expect the main gui login screen to come up, but nothing happens. I have tried other options such as trying to run it through cmd (it gave me an access error) and the HKEY_CLASSES_ROOT\jarfile has the command: "C:\Program Files\Java\jdk-11.0.2\bin\javaw.exe" -jar "%1" %* already.
Cant comment quite yet on other peoples posts so ill just submit an answer, tho its gonna be more of a swing and miss probably.
Id recommend opening that jar file with a java decompiler (The one i had in mind is jd-gui) and checking the integrity of the code, file system, and, in general, just check arround for common errors when packaging, maybe you included something you shouldnt in the class and hence why the problem, or its crashing on load cause of a missing reference you had linked to with a relative path which got broken upon moving the file, those are the two big ones that come to mind that you should check first upon opening the file, other than that, i cant provide much more insight, godspeed to you!

runnable .jar file runs on my windows device but won't on others(windows too)

So I've made up a little programm for my mom to use. I exported it into a runnable .jar, which is a first for me, so I obviously screwed it up.
Actually the file works just fine on my computer. But when I try it on hers, double clicking on the file just gives me an error : "could not find Main class : controller.Main". And the programm won't start
I used eclipse GUI to export it (right click on projet -> export -> runnable JAR), and it gave out a file working well on my computer.
If it is of any help, my programm uses a bunch of resources files, but I included them in the right place in the folder that I gave to my mother so it should not be the problem (besides, it would glitch out when trying to use the images, but not stop it from launching, according to some personnal tests)
What can be the Reason for it to work on my device, but not another one (both Windows) ? And how to fix this ?
N.B: I have included a MANIFEST.MS file in my project before exporting, but Eclipse didn't make any explicit mention to it when exporting, so I think it didn't take it into account. Besides, I reffered to my main class as Main (and not controller.Main as in the error message I got on my mother's computer). This was probably wrong of me, but I think it shows that Eclipse didn't care much for my manifest and kinda made his own.
Thanks in advance !
If you want a link to download the folder, to check it out yourself, let me know ! (it's a bit heavy though, there's quite a lot of images)
EDIT : I get different error messages with different computer. One computer would give me this first message :
first computer error
Another computer gives me those 2 error messages (one after another)
2nd computer error (1/2)
2nd computer error 2/2
And of course, it still runs fine on mine, just with a double click !
My .jar file (opened as an archive) contains :
3 folders corresponding to my packages : controller, model, view
1 folder META-INF containing the MANIFEST.MF generated by eclipse, looking like so :
Manifest-Version: 1.0
Main-Class: controller.Main
Class-Path: .
Ok ! I could figure it out. So there were actually 2 problems on top of each other :
first, I think Martin's suggestion was correct. After researches on the new error message, it became clear that native calls had probably had Something to do with the problem, although I have no idea what native Library I was using. I could solve this first part of the problem by selecting another option to handle libraries in the eclipse export feature : I chose the option "package required libraries into genereated JAR" (instead of "extract required libraries into generated JAR" before), and it make the error message go away !
Even though the message disappeared, the app would still not launch. But the problem was more obvious this time; I Simply compiled and exported the file with a java 9 JDK for eclipse, while trying to run it on a java 8 software (I think java 9 is only available in its developper version to this day). So I just downloaded java 8 JDK, configured the build path to use the latter, and it finally worked on the other computer.
Thanks to Martin who helped me get on track with the right questions !

Double clicking jar file wont execute after update JRE

As the title says, my jar file don't executes when i double click on it.
Before I updated the JRE and exported the runnable jar file it worked perfectly fine.
But after the update when i extracted the same project not a single line of code runs.
I have tried the existing answers here with no luck so i decided to ask my own question. My project is a game with pictures, music and textfiles and yes they have the same paths from the working jar.
Regards
Edit:
So i moved my project to a new computer with JDK 1.8.0.77 and extracted a runnable jar file but without any difference. However on this computer I can extract another project containing pictures etc and it's working perfectly fine.
The problem might be in my code then but the error must be early because no display comes up and the process doesn't show under processes. How can i debug jar files?
So the problem was my code all along, one of my 100 classes was creating a textfile and it seems like the jar file crashes when it did.
Sorry for taking up your time but I appreciates your answers. Now I have better understandings using JRE/JDK and jar files.
Thanks

Running .jar File Java 8

I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.

Netbeans plugin works perfectly while debugging, but not working when the plugin is installed to the IDE

I am currently working on a Java plugin for Netbeans 6.9.1 using the Apache POI api for interfacing with Microsoft documents that has two functions, the first takes data from a properties text file and creates an excel spreadsheet(.xlsx) containing that data, and the second part takes text from a spreadsheet and adds it to a properties text file.
When I run the code in the debugger both of the functions work great. The files get created and edited as they are supposed to, but I ran into problems when I created the NBM from the project and installed the plugin. The import (from .xlsx to .txt) does not do change the contents of the text file. I have checked variables at various times in the code to make sure there's nothing wrong with them there and everything was as it should. The export (.txt to .xlsx) works fine in the plugin, so I'm not sure exactly what's going on. When I start the import it goes through the steps, but nothing ever gets changed or written to the final files.
I'm pretty new to Netbeans so I'm not sure if there is a major difference from debugger to the actual plugin that is affecting how the program acts. Any help
The problem was creating a temporary file to write to. In the debugger it wrote to the projects folder, whereas when it was installed as a plugin it was creating the temporary file in Program Files. Windows wasn't letting the program get at the file once it was created in that directory so I changed it to create the file in Local App Data instead.
$ System.getProperty("user.home") + "\\Local Settings\\Application Data"
Glad that it's finally working, wasted a lot of time on such a simple fix, hope this can help someone else in the future.

Categories