Results 1 to 8 of 8
Thread: Opening .html file
- 08-05-2010, 01:41 PM #1
Opening .html file
Hi
I want to open index.html file when I click on the menu item of the GUI, this file is in dist/javadoc/index.html of my GUI application project.
I tried
Here I didn't gave the full path because I thought it might give problem when this application jar file is shifted to next PC.Java Code:try { Desktop.getDesktop().open(new File("dist/javadoc/index.html")); } catch (IOException ioe) { ioe.printStackTrace(); JOptionPane.showMessageDialog(null, "Can't open requested file.", "Error", JOptionPane.ERROR_MESSAGE); }
I don't have much idea on File IO operation, so any help is appreciated.
-Regards
- 08-05-2010, 02:03 PM #2
What happens when you execute the code?
For debugging, take the new File() out of the method call, create a File object and printout its absolute path to see if the path to the file is correct.
- 08-06-2010, 06:24 AM #3
When executed, I got error message saying file don't exist. So changed code and I did this
I got it working,the README.txt is open, but still one issue is left.Java Code:File file = new File("C:/Users/prajin/Documents/NetBeansProjects/MMS/dist/README.TXT"); System.out.println(""); System.out.println(file.canExecute()); System.out.println( file.getPath()); Desktop.getDesktop().open(file);
Using absolute path to open file is not what I want, because README.txt always have to in same directory.
If I move the application to D: drive with README.TXT along, then absolute path won't work. What I'm looking is relative path, that will search the application directory and open readme.txt.
-Regards
- 08-06-2010, 06:27 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Where is your java class file is located?
Is this done on a NetBeans project?
- 08-06-2010, 06:45 AM #5
Yup it's NetBeans project.
But I got it working,
Now where ever application is shifted, file will open. Just I have to make sure that readme.txt file is inside application directory.Java Code:String currentDirectory = System.getProperty("user.dir"); System.out.println(currentDirectory); File file = new File(currentDirectory+File.separator+"dist/README.TXT"); System.out.println(file.getName()); System.out.println( file.canExecute()); System.out.println(file.getPath()); Desktop.getDesktop().open(file);
Thanks for the help
- 08-06-2010, 06:58 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
The things is this. Do you ever heard about relative paths and absolute paths we are talking about in almost all languages. You've access the file in wrong way in *.html case. Use of text file follows the complete file, where system can easily locate it.
- 08-06-2010, 07:05 AM #7
Yeah I have heard about absolute and relative path. And for *.html case, I was doing it wrong, I agree.
- 08-06-2010, 07:20 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Both dist and src folders are in same level, and you want to move your file handler one step ahead. :) That's what you haven't done.
Similar Threads
-
Error While Opening A PDF file
By Cluster Storm in forum AWT / SwingReplies: 6Last Post: 06-07-2010, 06:03 PM -
Opening applet with .html file
By Atriamax in forum New To JavaReplies: 12Last Post: 09-13-2009, 06:26 AM -
Opening a custom proyect file
By janpol1 in forum EclipseReplies: 0Last Post: 02-04-2009, 02:29 PM -
How can I include a html file in html textarea?
By surya_dks in forum New To JavaReplies: 2Last Post: 10-04-2008, 07:20 AM -
Problem in opening a file
By Raghav kv in forum New To JavaReplies: 1Last Post: 08-11-2007, 01:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks