Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How can I get the path of the desktop directory?  (Read 4275 times)
0 Members and 1 Guest are viewing this topic.
Offline gouessej
« Posted 2010-12-05 16:50:32 »

Hi!

I try to copy a shortcut file into the desktop. I have made a lot of research about this.

On Linux, I check if XDG_DESKTOP_DIR is set and non empty. If not, I check if $HOME/.config/user-dirs.dirs exists; if so, I call ". $HOME/.config/user-dirs.dirs". Then, I can use System.getenv("XDG_DESKTOP_DIR"). Using directly XDG_DESKTOP_DIR is enough on GNOME but not on KDE (.config/user-dirs.dirs is useful in this case).

On Mac, I'm not sure $HOME/Desktop works for non English users. Does someone have a better idea? I don't really know AppleScript...

On Windows, I have found this but there is a missing class StreamReader:
http://stackoverflow.com/questions/1080634/how-to-get-the-desktop-path-in-java/1080900#1080900

This does not work always:
1  
2  
3  
4  
5  
FileSystemView filesys = FileSystemView.getFileSystemView();

File[] roots = filesys.getRoots();

filesys.getHomeDirectory();

I could use SHGetFolderLocation or SHGetFolderPath APIs but I prefer avoiding native calls. Does someone have a better idea?

The Java Web Start shortcut feature has too much bugs including this one and this other one, I cannot use it. It has never worked reliably on KDE. Thank you very much for your attention.

Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2010-12-05 18:16:12 »

I think StreamReader should be InputStreamReader.

Cas Smiley

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #2 - Posted 2010-12-05 18:31:14 »

StreamReader is defined in the code he posted :p

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #3 - Posted 2010-12-05 18:33:47 »

StreamReader is defined in the code he posted :p

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }
}

Thanks... I was a bit... absent-minded  Lips Sealed

Offline pjt33
« Reply #4 - Posted 2010-12-05 18:44:15 »

What are you trying to do which requires you to find the desktop folder without asking the user where it is?
Offline gouessej
« Reply #5 - Posted 2010-12-05 19:49:05 »

What are you trying to do which requires you to find the desktop folder without asking the user where it is?
I'm trying to create a shortcut on the desktop. This feature is quite broken in Java Web Start. Mine is almost currently ready for Linux, Windows will require a bit more work and I have no idea of how to implement it on Mac.

Offline JL235

JGO Coder


Medals: 10



« Reply #6 - Posted 2010-12-05 20:04:02 »

Just to throw it out there, there are other ways to get the desktop on Windows via Batch to WShell or the registry here.

I personally would use the WShell method as I believe registry access is more likely to be locked down.

If all your checks fail you can also just fall back onto checking if '~/desktop' folder exists as if it does then it's probably going to be their desktop (although yes, this is far from certain but the fact the folder exists does make it more likely).

Offline gouessej
« Reply #7 - Posted 2010-12-05 20:55:58 »

Just to throw it out there, there are other ways to get the desktop on Windows via Batch to WShell or the registry here.

I personally would use the WShell method as I believe registry access is more likely to be locked down.

If all your checks fail you can also just fall back onto checking if '~/desktop' folder exists as if it does then it's probably going to be their desktop (although yes, this is far from certain but the fact the folder exists does make it more likely).
%USERPROFILE% is not present on very old Windows (95, 98). '~/desktop' folder exists only on English desktops, I don't know on which versions of Windows WShell is available. Maybe I should find an heuristic method if the registry method fails.

Fortunately, 60% of the people who try TUER are on Linux  Smiley

Edit.: WShell is present on Windows since Windows 98.

Offline lhkbob

JGO Knight


Medals: 32



« Reply #8 - Posted 2010-12-05 23:23:50 »

Why not have a couple of default checks, such as '~/Desktop/' or the equivalent on Windows.  If that doesn't exist, then just ask the user where they'd like to put the shortcut (and you could write it somewhere in case they install a second game of yours and you could just look it up). 

This way, you get the common case really easily but you have a valid fallback for the weird OS installs, old versions or non-English OS's (which you won't be able to get correct all of the time anyway).

Offline gouessej
« Reply #9 - Posted 2010-12-05 23:51:40 »

Why not have a couple of default checks, such as '~/Desktop/' or the equivalent on Windows.  If that doesn't exist, then just ask the user where they'd like to put the shortcut (and you could write it somewhere in case they install a second game of yours and you could just look it up). 

This way, you get the common case really easily but you have a valid fallback for the weird OS installs, old versions or non-English OS's (which you won't be able to get correct all of the time anyway).
Actually the registry method rarely fails. JL235's suggestion would be an excellent fallback, I would like to use such a source code but in Java:
1  
2  
3  
4  
5  
use Win32::OLE;

  my $wsh = new Win32::OLE 'WScript.Shell';

  my $desktop_path = $wsh->SpecialFolders('Desktop');

I don't want to prompt the user.

I could use this:
1  
WScript.Echo(WScript.SpecialFolders("Desktop"));


If I do this:
1  
wscript //NoLogo //B

does a Windows user see a logo or a command display when I use WScript?

I have found no way of getting the desktop directory on Mac OS X. Is there a Mac user here?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #10 - Posted 2010-12-06 10:45:50 »

Hi!

On Mac OS X, whatever the locale, it seems to be:
Mac OS X/Users/<username>/Library/Desktop
I can get this with System.getProperty("user.home")+"/Desktop".
On Mac OS 9, it seems to be:
Macintosh HD:Users:<User Name>:Desktop Folder

Using the registry on Windows might cause some localization problems and the use of "Shell Folders" registry key is deprecated since Windows Vista then I will use WShell.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2010-12-06 12:59:27 »

In Vista/7 the directory ./Desktop always exists.

For different locales, it is hidden, and a symlink (with a localized name) to it is created.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gouessej
« Reply #12 - Posted 2010-12-06 13:01:42 »

In Vista/7 the directory ./Desktop always exists.

For different locales, it is hidden, and a symlink (with a localized name) to it is created.
What happens if you move your desktop somewhere else? Thank you for this explanation as I rarely use Windows, I'm a bit lost and I have no way of testing the WShell script.

Offline JL235

JGO Coder


Medals: 10



« Reply #13 - Posted 2010-12-06 13:09:33 »

%USERPROFILE% is not present on very old Windows (95, 98). '~/desktop' folder exists only on English desktops
If your making a game then will it even run on a machine still running Windows 95 or 98; will they even have a recent version of Java installed? I'd presume it's there but fall back onto asking or doing nothing if it's not.

What is the likely hood of a pre-Vista Windows user (English or non-English) having a folder called 'Desktop' in their home directory which isn't their desktop? I'd bet it's extremely low. If it's there and you used it then I'd imagine it's the right place 99.9% of the time (although again I'd only do this as a fall back option).

Offline gouessej
« Reply #14 - Posted 2010-12-06 13:40:12 »

If your making a game then will it even run on a machine still running Windows 95 or 98; will they even have a recent version of Java installed? I'd presume it's there but fall back onto asking or doing nothing if it's not.
%USERPROFILE% is not localized, it cannot be used but you're right, I'm even not sure it is possible to install Java 1.6 on Windows 95.

What is the likely hood of a pre-Vista Windows user (English or non-English) having a folder called 'Desktop' in their home directory which isn't their desktop? I'd bet it's extremely low. If it's there and you used it then I'd imagine it's the right place 99.9% of the time (although again I'd only do this as a fall back option).
Lots of people still use Windows XP and I have a lot of French users (as I'm French). I suggest using the following algorithm:
- use WScript.SpecialFolders("Desktop") as it is the most reliable and not deprecated way of getting this piece of information since Windows 98
if it does not work
     - use the registry method and replace the environment variables by their values (as I did for Linux)
     if it does not work
          - try to use System.getProperty("user.home")+"\Desktop" as Riven says this directory always exists on Vista and 7

Can I use Runtime.getRuntime().exec() to execute the WShell command lines? What displays WScript.Echo(WScript.SpecialFolders("Desktop")); on your machine?

Edit.: Java 1.6 cannot be installed on Windows 98 but still on Windows 2000.
Edit.2: I will use a simple bash script on Mac as it seems very complicated to create an Alias

Offline lhkbob

JGO Knight


Medals: 32



« Reply #15 - Posted 2010-12-06 16:49:12 »

The Mac's desktop is always located at '~/Desktop' for English versions, I'm not sure if it's different for non-English but Mac has a pretty rigid folder convention.  The is always located in /Users/<username/Desktop (you had an extra Library folder there, which is incorrect).  Reading here (http://osx.tribe.net/thread/ea74316d-e1f6-4de7-a518-0836864bc7fb) it appears that within the Terminal, localized installs still use paths named Desktop and just display the translated version with the GUI folder environment.

Offline gouessej
« Reply #16 - Posted 2010-12-06 20:34:04 »

The Mac's desktop is always located at '~/Desktop' for English versions, I'm not sure if it's different for non-English but Mac has a pretty rigid folder convention.  The is always located in /Users/<username/Desktop (you had an extra Library folder there, which is incorrect).  Reading here (http://osx.tribe.net/thread/ea74316d-e1f6-4de7-a518-0836864bc7fb) it appears that within the Terminal, localized installs still use paths named Desktop and just display the translated version with the GUI folder environment.
It is not different for French users  Grin

Creating an alias (equivalent of a shortcut file on Mac) is so complicated, I'm going to use a simple bash script.

Offline gouessej
« Reply #17 - Posted 2010-12-09 15:43:47 »

Hi

As far as I know, my implementation works on Linux, Mac and Windows. It has not been tested on Unix. Thank you for your help Smiley Look at my SVN repository to find the source code:
http://tuer.svn.sourceforge.net/viewvc/tuer/pre_beta/engine/DesktopIntegration.java?view=markup

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!