File manipulation won't work in Java Web Start - java

I built this program that works with files. Since I made it for a friend, I made it into a Java Web Start app, complete with a JNLP file.
When I launch the app through an ANT (netbeans) without the JNLP, it works just fine. But when launched through the JNLP (even through netbeans), the button that is supposed to perform the required actions instead does nothing (it just stays in "clicked" mode until you hover away from it).
I spent hours trying to figure out the problem, but no luck.
Here is the problematic method:
public void copy(String path1, String path2) throws IOException {
File inputWorkbook = new File(inputFile);
Path in = Paths.get(path1);
Path out = Paths.get(path2);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
.
.
.
If I don't attempt to do anything with the inputWorkbook file, everything works ok. As soon as I attempt ANY sort of method on it (such as w = Workbook.getWorkbook(inputWorkbook), or even inputWorkbook.exsists();) , the problem occurs. It won't even throw an exception, it just does nothing... Again, the problem only occurs when the program is launched through the JNLP file.
I hope I managed to explain the problem... I'm new to programming.
Thank you!!!
Adam

Webstart applications run in a security sandbox which prevents the to access the file system. You need to digitally sign your har to gain access to the file system, or use the file open api. See http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/faq.html#302 and http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#FileOpenService for more information.

Related

Java File Not Opening Eclipse

I recently finished working on a project and I went to create a runnable JAR file out of it, but the problem is, as a runnable JAR it never opens. When I run the program from Eclipse it opens fast and without any errors. When I open the program as a runnable JAR file it shows that the JAR file is open, however, a display is not shown and I cannot click on the file at all.
I saw a forum post where a user was told to type java -jar <FILE NAME>.jar and they would be able to figure out why their program wasn't opening from the output issued by the command. I actually tried this to see if it would work for me as well, but I get an error Unable to access jarfile <FILE NAME>.jar. I was running this on Windows so I quickly sent the entire folder to my Mac laptop and booted Eclipse and the same thing happened. I was able to run the program from Eclipse with no issues, but outside Eclipse I never got a display.
The program that I am creating is for AP Computer Science students for when I am their TA in the upcoming school year, so I really need it to work. The only issue that I could see is making the JAR file unable to launch is when I read information from a text file but even that should not be an issue. Any help would be appreciated. If you need any code I can post it right away. Thank you in advance.
EDIT: By the way, the reason why I believe that it might be how I read information from a text file is because previously to read a file I would place it in the same location that the runnable JAR file. This, however, looks very ugly so I decided to try and incorporate the text file from within the resources folder that is apart of the Java source folder. To read in from a text file I use the following code:
public int getHighscore() {
ClassLoader classLoader = this.getClass().getClassLoader();
File file = null;
Scanner scanner = null;
int highScore = 0;
try {
file = new File(classLoader.getResource("Text/Highscore.txt").toURI());
scanner = new Scanner(file);
while(scanner.hasNextLine())
highScore = Integer.valueOf(scanner.nextLine());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return highScore;
}
EDIT #2: I tried running the JAR file from the command prompt as an administrator on my Windows computer and I also tried running the JAR file from terminal on my Mac. The interesting part is that both times the file would not run, I received the error message Unable to access jarfile <filename>.jar, but when I tried opening a different JAR file that was in the same directory as the file that I am having problems with, it opened fine. I wanted to make sure that it wasn't a system-wide issue that I was having, and it's not. For some reason, this is the one and only Jar file that just will not open.
EDIT #3: Alright, so I deleted the code that I had to get the user's highscore (getHighScore) and the code to write a user's highscore (setHighScore) and I always deleted the Highscore.txt file that I stored within my resources folder and I ran the program as a last hail-marry attempt to try and figure out why the program was not running, and it ran. So the problem was 100% how I am reading/writing to a text file. Thanks everyone for their time and effort in attempting to help me figure out my problem, means a lot.

How To Open Files With .Jar (Java) Application?

Tough question here. How do you open a file in java, in the way that when you double click on the file it automatically opens in a java application.
I'm making a musicplayer (first real big Java project for me) and I have no clue how to acheive this. When you open, lets say, a .mp3 file it will open in whatever default program you have selected for it (such as VLC mediaplayer or Windows Media Player). What I want is to be able to set the .jar file of my application as the default program for .mp3 files, and then to be able to actually launch the files in the application.
When I currently try to open a file with the application I get a windows error saying "This app cannot be executed on your pc". But when I launch the .jar itself without doing it by trying to open a .mp3 file it runs just fine.
Does anyone know how to acheive what I want? Many thanks in advance!
---edit---
I do not mean that you can select a default program for the mp3 file. The problem is that windows throws the error shown above, and that I dont know how to handle the application being launched by opening a file (which does not ope due to the error).
I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)
BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:
if (args.length > 0) {
File f = new File(args[0]);
start_the_application_with_a_file(f);
} else {
start_the_application_without_a_file();
}
C:\ProgramData\Oracle\Java\javapath\java.exe -jar "C:\Program Files\YourApp.jar" %* within a batch-file (.cmd) might do it.

Processing 2.0 Movie cannot open movie file when running from jar

I'm trying to run a movie using the Processing 2.0 Movie class.
If I run my program from my code editor (Eclipse in my case) everything works fine and the code runs flawlessly: it locates and starts the movie.
However, when I export my program to a jar and start it from my terminal, it does locate the file but it crashes on trying to open it.
I am currently using this code to find my movie path string.
public String locateMovie(String moviePath) {
String movie = MoviePlayer.class.getClassLoader().getResource(moviePath).getPath();
if (movie == null) {
System.out.println("FATAL ERROR --- Movie file not found: '" + moviePath + "'");
}
return movie;
}
As you can see I am using the classLoader to locate the file for me since the paths are different after I export it to a .jar file and do not wish to change the paths all the time myself. The .getPath() deletes the 'File:' in File:/Path/To/My/File as this is an unneeded side effect from using the above way.
A bit further in my code I've already added an extra check to see if nothing is wrong with the path that the classLoader is returning, I do this with the following lines.
File temp = new File(moviePath);
System.out.println("If I have something after this, the file exists: '" + temp + "'");
It always returns the right path, whether it's running from within my Eclipse (/Users/Path/To/My/Folder/2/java/Game/movies/introMovie.mp4) or from the executable .jar file (introMovie.mp4).
At first I thought it was strange that in the .jar file it was giving me a file straight away without the path leading to it because it does have some structure in my project but after using the
jar tf myProject.jar
command in my commandLine it returns the same file/pathname for the file so the path should be correct and all is still well here.
But now, when I try to load the movie via Processing' Movie class with the following code:
currentMovie = new Movie(parent, moviePath);
It throws an java.lang.StringIndexOutOfBoundsException exception and crashes. I have no idea what i'm doing wrong since this code runs flawlessly from within my code editor + I'm using the same way of locating files for my audio files and those run aswel, even from the executable .jar file.
As far as I can tell from reading the Movie class reference library it just needs a place to display it on and the path to the file which I am both giving.
I was hoping some veteran here could see what I am doing wrong and help me solve this problem that I'm been struggling with for a few days now. It's probably something stupid that I'm just overlooking right now.
I don't know exactly what's wrong but here's a couple things I would do:
check the permissions on the video file, make sure the program would have access to it.
I've read that it's a better idea to use
Thread.currentThread().getContextClassLoader()
than the class's classloader, but I don't know if that would help.
Processing libraries are open source. Maybe see exactly where that exception is being thrown and take a look at the source. It might help you pinpoint the issue.

JavaFX loading external resources problem

I've coded small JavaFX Applet in Netbeans 6.8 IDE. Everything works fine if the applet runs on my computer. But when I put the applet (and edited JNLP files containing changed paths to server) into server, it doesn't load any data from an server text file. Paths are correct - i look at it many times - they are ok. It also doesn't load images with external urls. What is wrong?
Haven't seen your code so I'm going to make a guess. If your resource is not bundled in your JAR file and you are using file:// to access it, then you will need to sign your applet. On NetBeans, right click on project node -> properties -> application. Select self signed.
If you don't want to sign your applet, then access your resource as REST. Use the HttpRequest.
If it is on the client machine, consider using JNLP APIs like so.
FileOpenService fos = (FileOpenService)ServiceManager
.lookup(“javax.jnlp.FileOpenService”);
//Open dialog pops up
FileContent fc = fos.openFileDialog(null, null);
The best way to diagnose the problem is to open the Java Console and see if there are any exceptions. Run $JAVA_HOME/bin/ControlPanel -> Advanced -> Java Console -> Show Console

Trying to get the files from a directory using list() or listFiles() in java

I am working on a small basic GUI program that gets the files from my resources and gets the names of the files and places them in a combo box. for example i just have a file inside the same package called images and trying to get the files names. the way I get the file is by using the getResoure() like so
java.net.URL url = FileDemo.class.getResource("images");
but when I try to get the files inside the directory
File urlfile = new File(url.toString());
String[] files = urlfile.list();
the first line will convert the url to string and create a file object but when I try to get the list of files inside the directory it returns a null to the array of strings.
I broke down the code and used the debugger in netbeans found out, when it did the SecurityManager check it wouldn't pass.
My question is are the files inside the project protected or there is no way to access them using the list() and listFiles() or am i doing something wrong? Also I ran the same script on my schools computer which they have windows 7 the code above worked. But when i ran it on my mac and even 2 win xp machines it just didn't work? why is that ?
I hope this makes sense i am just stuck here still a new to java
Thanks in Advance.
The class to getResource(String) is returning a URL to images but there's no guarantee that this is a file URL (beginning "file:"). If your class is embedded within a jar file this certainly won't be the case, hence why it fails in some situations and not others.
If your intention here is to actually make a portion of a file system visible to the user then I'd recommend avoiding getResource altogether, which is typically more useful when your application wishes to locate and load resources such as icons or config files.
Instead, you could use JFileChooser, which is a rich Swing component for navigating the file system and selecting files.

Categories