Java: Run/Open/Edit any file - java

Using Java program I need to run/open/edit any file. This should have similar effect of double clicking file in File Explorer and OS will execute file if it an executable OR open/edit it in it's respective registered program.
I have tried the Runtime.exec() method (See down there) but that method only runs executable files. I need mine to run any file. This includes text files, audio files, pictures, anything.
I have tried the following:
Runtime.getRuntime().exec("README.txt");

Have you consider trying to use the java.awt.Desktop class?
For example...
if (Desktop.isDesktopSupported()) {
try {
if (Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
Desktop.getDesktop().edit(new File("Readme.txt"));
}
// or...
if (Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
Desktop.getDesktop().open(new File("Readme.txt"));
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
This will attempt to open/edit the file in the OS specified editor for the given file

Related

Is there a way to have Java run a file it creates?

I'm new to java, and I'm trying to get a Java program to create a .html file, put some code in it, then run it. So far I can get it to create the file and it runs correctly, but is there a way to have the Java program automatically run the .html file or must it be done manually?
So, it really depends on on what do you mean by "Running it", you can either:
Create a webview and load in the file
Or you can make the default browser load in the file and display it like so:
try {
Runtime.getRuntime().exec("cmd.exe /c start c:/path/to/html/file");
} catch (IOException e) {
e.printStackTrace(); // if something goes wrong
}

Sound will not play in JAR file

I have a problem and I hope you can help me.
Some talk about what I am doing so you know what's going on: So at the moment I'm trying to program a litte piece of software which can play me some music files (mp3 files to be exact, so i'm using the jLayer API). I'm working with Netbeans and I have succesfully imported a music file in the project. If I build my program and open the resulting JAR file with an archive program, I can find my music file in there. My function which I'm using goes like this:
public static String play(String file) {
File test = new File(file);
try {
FileInputStream in = new FileInputStream(test);
Player pl = new Player(in);
pl.play();
return "success";
}
catch (Exception e) {
return e.toString();
}
}
As you can see I'm getting a String with the Path Name and refactor him so I can play the file. I'm calling the function with the following code (the music file is saved in the ressources package):
MP3.play(getClass().getResource("/ressources/angel.mp3").getPath())
So if I start the programm via Netbeans everything works perfectly fine. But if I create a JAR file and start the program nothing happens. The Exception getting is the following:
java.io.FileNotFoundException: file:\C:\Users\Raphael\Documents\NetBeansProjects\MP3\dist\MP3.jar!\ressources\angel.mp3
It says the File does not exist but if I check my JAR the file is there......
Another strange thing I found out is the following: If I use the following function to play the music file:
public static String play(InputStream test) {
try {
Player pl = new Player(test);
pl.play();
return "success";
}
catch (Exception e) {
return e.toString();
}
}
and call the function with the following argument:
MP3.play(getClass().getResourceAsStream("/ressources/angel.mp3"));
Everything works fine in both Netbeans and the final JAR. Can anybody explain me what I'm doing wrong and only the second function works in the JAR version?
It would be really nice if you could help me in this matter.
Greetings,
xXKnightRiderXx
I am assuming that you have 2 packages 1 is src where your .java files is located and other is resources where your sound files is located
So i suggest you to use
MP3.play(getClass().getResourceAsStream("/angel.mp3"));
Because GetResource() automatically finds the resource package

How do i open a PDF file in Netbeans?

I want my application to open a pdf file when I click a dedicated button. How would i approach this? Also if I run the application from netbeans it shows the pdf but when compiled nothing comes up?
My code
private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File("ana.pdf");
try {
Desktop.getDesktop().open(f);
} catch (Exception ex) {
System.out.println(ex);}
You can explicitly give the entire file path, which might solve your problem. Also the OS you are using must support the operation. This might help:
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("C:\\Users\\klinks\\Documents\\pdf.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException e) {
// System probably doesn't have a default PDF program
}
}
Your code gets the file from the current directory. The file is there when you run it from netbeans, but the file is not there when you run it.
Unfortunately, there's no easy way to do this. I think the best idea would be write the documentation as HTML, put it on a server, and open the web browser (using Desktop.browse). If someone else has a better idea, please comment.

How to load a PDF from a .jar File?

I have made a Swing application and will include a file, help.pdf in the .jar file. When the user selects Help->User Guide from a JMenuItem, it should load the file in the default PDF viewer on the system.
I have the code to load the PDF,
private void openHelp() {
try {
java.net.URL helpFile = getClass().getClassLoader().getResource("help.pdf");
File pdfFile = new File(helpFile.getPath());
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File does not exist!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
This works in the eclipse IDE, however, when I pack it into a jar for other people it no longer works.
How do I fix this problem?
The problem is that a File cannot name a component of a JAR file. What you need to do is to copy the resource from the JAR file into a temporary file in the filesystem, and open using the File for the temporary file.
File names in a .jar file are case sensitive. In your text you write Help.pdf but in the code you use help.pdf. The upper/lowercase in the Java code must match the case of the file, even if you are using a system where the filesystem is not case sensitive.
Try
getResource("Help.pdf");
instead (assuming the filename in your posting text is correct)
I think you have to retrieve the location of the jar, open it and load the pdf file from within your application. The .jar file is just a zipped archive, which can be read with java easily...

Runnable jar runs fine with java -jar, but problems with double-click

I am using the eclipse IDE and I just exported my project using 'export->Runnable Jar'. In my code, I have loaded a map using
URL map1url = this.getClass().getResource("map01.txt");
and later
inputmap = new File(map1url.getFile());
and then scanned its data using
Scanner sc = null;
try {
sc = new Scanner(inputmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Now, when I package the map01.txt into the jar, and double click it, it runs, but can't find the map01.txt. When I use java -jar, it runs, but still can't find the map.
When I place map01.txt in the same directory as the jar file, but not actually in the jar, and then double-click it, it runs, but doesn't find the map. If I run it using java -jar it runs and loads the map. What is the cause of this problem? How can I fix it? And the map01.txt is the only resource that doesn't work. I have loaded many images that are placed into the jar file, and they all load and display fine. For the images I use
URL url = this.getClass().getResource("myImage.gif");
try {
BufferedImage myImage = ImageIO.read(url);
} catch (IOException e) {
}
Why do they work and not my map? How can I fix this so I can package ALL my resources into one jar and then double-click it to run?
When you say "new File(map1url.getFile())", you're ending up with a java.io.File that refers to a file in the current directory. If the file actually is in the current directory of the process (which will happen if the file in in the current directory of your shell and you use "java -jar", then this will work; otherwise, it won't.
Why not just use getResource().openStream() to read the map file?

Categories