Java File Not Opening Eclipse - java

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.

Related

Cannot write file to Documents folder

I'm trying to write to a file in Java, or create a new file if the file doesn't exist. (Using JDK 14). However, when I run the following code I get an IOException at the if statement condition that reads The system could not find the file specified if the file doesn't exist, and Access is denied if the file does.
File file = new File(filePath);
System.out.println(filePath); // C:\Users\username\Documents\test.txt
if (file.createNewFile()) {
System.out.println("File successfully created");
} else {
System.out.println("File already exists");
}
The code works when I attempt to save it to the desktop folder and saves the file successfully, but for whatever reason isn't allowed to touch Documents.
The user I'm running IntelliJ as has full access to all files on the computer, and running the IDE as administrator did not fix the problem. However, I can save to the user folder and the desktop. It is only Documents or child directories of it that I can't save to.
There are a few similar questions on the site such as this one, however the cause is not the same as in my case this is a permissions issue, and not an issue of a missing directory.
I've just hit this same issue. It seems that JFileChooser() on some Windows 10 installations doesn't tell the os that the user has selected a folder and as such Sandboxing, Malware Control, Access control blocks access to create a file even though the user had full access (permission checks are OK but file write fails with IOException 13 or Access Denied). However FileDialog() DOES work where JFileChooser fails...
Heres what I would do to try to debug this:
Should you be trying to save to 'My Documents' as opposed to 'Documents'?
Try to save the file to C:\Users\username\test.txt (I suspect that will work)
Try to save the file to C:\Users\username\My Documents\test.txt
If you really do want to save it to 'Documents' make sure the 'Documents' director exists.
If thats the case, open the properties on Documents under the security tab select Everyone under 'Group our user names' and 'Full control' under permissions. that will open it wide up and should allow file creation. you might want to take note of what the settings where so you can put them back as setting a directory to wide open permissions can be 'problematic'. try running the program again.
1
if you want write data to file just user any Writer For example:
FileWriter writer = new FileWriter(yourFile);
You can use stream filtering for faster action.
Show more your code. You've just showed condition for existing file.
2
if you want create file in Documents folder, get a path, then make a:
File file = new File(documentPath);
while(!file.exists())
file.createNewFile();
//condition for file existing...`
If I don't help, comment below, just I can't understand your question :). Good Luck

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.

Illegal Argument Exception: URI is not hierarchical with runable .jar file

I know this question has been asked a bunch of times before, but I'm peeled through all the other threads and tried a bunch of stuff, but can't find anything that resolves my issue. I have a program that compiles and runs without issue in Eclipse, but when I export a runnable .jar file, it won't launch. I tried running it from the cmd prompt, and got the error Illegal Argument Exception: URI in not hierarchical. This is happening in an included sound file which I have as a classpath resource. The code is like this:
try {
pop = new File(IntroView.class.getResource("/model/pop.wav")
.toURI());
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
From what I've read it's a problem with the way that the file is being packed up into the .jar, but I'm having a hard time wrapping my head around it. Can anybody shed some light on this and possibly provide a solution? Thanks.
I am sorry but it seems you cannot represent a File object from inside a JAR. When locating a file using File object it checks for file in the OS directory structure only. The File object can locate the JAR itself in a directory but not what's inside.
You can get the InputStream to the file inside JAR like this as stated in a few places:
InputStream input = PlaySound.class.getResourceAsStream("Kalimba.mp3");
You could have these options:
Read the file from JAR and write it outside in your directory and
then get the File Object.
Extract the JAR to a folder and point to that with a File.
Simply get the InputStream and play the file as shown here:
How can I play sound in Java?
Alright so I got it working, but it's not really an ideal solution. What I ended up doing is creating a folder within the project, but outside of the source. So before, the resources were in
Project/src/Model/pop.wav
Now they are in
Project/Resources/pop.wav
I then just accessed then like this
pop = new File("Resources/pop.wav");
So as this stands, it still only works when launching from the IDE, but what I did was add a new folder within the same folder that the .jar is being run from which contained all the same resource files. The file reference looks for pop.wav relative to whichever directory the program(either in the IDE of from the .jar) is being run from, so it finds the files in this new folder and works fine. I don't feel it's the prettiest solution, but it works anyway.

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.

File manipulation won't work in Java Web Start

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.

Categories