Solved

Desktop File operations not working

Posted on 2010-08-13
29
868 Views
Last Modified: 2013-11-23
The code below does nothing. No file opened, no exception thrown.
What can I do to discover the problem?
It does execute the desktop.open(file) line.
Using JDK 1.16.0_81
Running Win XP
I'm running this from the Eclipse IDE.

Running the demo here:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
the browse and mail functionality works, but not the file functionality, again same result, just nothing.

package desktopdemo;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class MyDesktopDemo {

	public MyDesktopDemo() {

		// have tried .txt, .rtf, .pptx all with no effect
		String pathAndFileString = "C:\\my.pptx";
		File file = new File(pathAndFileString);
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			if (desktop.isSupported(Desktop.Action.OPEN )) {
				try {
					System.out.println("desktop.open(" + pathAndFileString + ")"); // gets here
					desktop.open(file); // but nothing happens
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			else {
				System.out.println("Desktop.Action.OPEN not supported");
			}
		}
		else {
			System.out.println("Desktop not supported");
		}

	}

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyDesktopDemo();
            }
        });
    }
    
}

Open in new window


0
Comment
Question by:allelopath
  • 9
  • 9
  • 8
  • +1
29 Comments
 
LVL 2

Expert Comment

by:VanNam
ID: 33431390
Hi Allelopath,

I've tested it with C:\\my.txt file, it opened file my.txt properly without any error. Have you got the right file that you want to open on your computer?

BR,
VanNam
0
 
LVL 1

Author Comment

by:allelopath
ID: 33431512
The file is definitely there. If I change the name to a non-existent file, eg. "myZZZ.pptx", then it throws an exception. I've also tried files with various extensions, like txt, pdf, rtf, with no difference.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 33431524
Are you certain no exception is thrown?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 33431686
If the answer to that is yes, you might like to try jdic: it does much the same thing, and i'd guess (i have to guess since there's probably no native source for  what you're currently using.

Looking at it and running it could give you some clues

https://jdic.dev.java.net
0
 
LVL 92

Expert Comment

by:objects
ID: 33434050
does the file open ok if you double click on it?
0
 
LVL 1

Author Comment

by:allelopath
ID: 33435010
>>does the file open ok if you double click on it?
yes
0
 
LVL 92

Expert Comment

by:objects
ID: 33435054
>             String pathAndFileString = "C:\\my.pptx";

does it still fail if you change that to:


            String pathAndFileString = "/my.pptx";
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33435617
Can you run your code from the command line (i.e. not in Eclipse) and tell us what you get?
0
 
LVL 2

Expert Comment

by:VanNam
ID: 33435811

Hi Allelopath,

When I change the file extension to 'abc', and got the error on the console windows:

Failed to open file:/C:/my.abc. Error message: No application is associated with the specified file for this operation.


And change the file to 'my.txt'. It's open ok, with the msg on the console windows:

desktop.open(C:\my.txt)


Maybe there's no application that is associated to your file?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33435820
>>Maybe there's no application that is associated to your file?

There is, or it wouldn't open on double-click
0
 
LVL 92

Expert Comment

by:objects
ID: 33438955
May well be a bug, there are a few Desktop on XP bugs in the db
0
 
LVL 1

Author Comment

by:allelopath
ID: 33438969
objects:
/my.pptx makes no differences. good idea though.
0
 
LVL 92

Expert Comment

by:objects
ID: 33438989
what service pack?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 33439564
What happened outside Eclipse ?
0
 
LVL 92

Expert Comment

by:objects
ID: 33439685
eclipse (or not) won't make a difference
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33439696
It could well make a difference to perceived errors
0
 
LVL 92

Expert Comment

by:objects
ID: 33439703
so please enlighten us with why?
0
 
LVL 1

Author Comment

by:allelopath
ID: 33442120
Microsoft Windows
Version 5.1 (Build 2600.xpsp_sp3_gdr100427-1636: Service Pack 3)
0
 
LVL 1

Author Comment

by:allelopath
ID: 33455510
CEHJ:
To assure there were no exceptions being missing, I added code to catch any exception:

package desktopdemo;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class MyDesktopDemo {

	public MyDesktopDemo() {

		// have tried .txt, .rtf, .pptx all with no effect
		String pathAndFileString = "/my.txt";
		File file = new File(pathAndFileString);
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			if (desktop.isSupported(Desktop.Action.OPEN )) {
				try {
					System.out.println("desktop.open(" + pathAndFileString + ")"); // gets here
					desktop.open(file); // but nothing happens
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (Exception e1) {
					e1.printStackTrace();
				}			}
			else {
				System.out.println("Desktop.Action.OPEN not supported");
			}
		}
		else {
			System.out.println("Desktop not supported");
		}

	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MyDesktopDemo();
			}
		});
	}

}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 33460042
> To assure there were no exceptions being missing

you're original code wouldn't miss any exceptions
still waiting on him to explain why he thinks it would
0
 
LVL 1

Author Comment

by:allelopath
ID: 33477449
I jar'd the class and ran it, with no difference (from running in Eclipse)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33477469
OK - worth a try
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33477508
If you were on Unix you could use strace. You might try this for Windows

http://www.intellectualheaven.com/default.asp?BH=projects&H=strace.htm

(or some alternative)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33477591
Have you tried simply upgrading (or marginally downgrading) the runtime?
0
 
LVL 1

Author Comment

by:allelopath
ID: 33477731
Currently using JRE 1.6.0_18, dont' want to go any further than _20 because of this:
http://it.slashdot.org/story/10/07/28/2121259/Oracles-Java-Company-Change-Breaks-Eclipse
but I could try _20.

0
 
LVL 92

Expert Comment

by:objects
ID: 33480831
most likely its a bug, lots of Desktop bugs on XP
0
 
LVL 2

Expert Comment

by:VanNam
ID: 33482750

You can try it on the another computer with windows xp (maybe virtual machine)
0
 
LVL 1

Author Comment

by:allelopath
ID: 33578640
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 33583869
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
how to add new optional parameter to JSP 1 49
by zero exception 10 52
What browser will run Java? 7 62
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question