2

In my Java program how can I find out where the Startup folder is on uers' PCs ? I know it's different on different versions of Windows. I only need my app to work on Windows. Any sample code ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Frank
  • 30,590
  • 58
  • 161
  • 244
  • Have you looked at http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java ? – barti_ddu Dec 31 '10 at 01:28
  • why do you need to find the start up folder on windows? Do you want your application to run at windows start up? – Ram Dec 31 '10 at 03:32
  • @Ram : See my question here : http://stackoverflow.com/questions/4565639/how-to-make-a-java-app-automatically-start-when-pc-starts – Frank Dec 31 '10 at 16:42

2 Answers2

1

This should work:

System.getProperty("user.dir")

here you have an overviwe about system properties:

http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • Didn't work. It should be something like this : C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup – Frank Dec 30 '10 at 22:18
  • Then what's the varying variable? the username? – RoflcoptrException Dec 30 '10 at 22:20
  • I don't quite understand your question, I want to find out where the startup folder is on users' PCs, so what property should I look for ? Or is there such a property ? I know the location of this folder is different on different versions of Windows. But there is no such a property as : user.startup_folder – Frank Dec 30 '10 at 22:27
  • 1
    I figured it out, it's a combination of user.dir and user.home – Frank Jan 05 '11 at 17:53
1

With ShellLink creating shortcuts is very simple. https://github.com/BlackOverlord666/mslinks

Maven:

<dependency>
  <groupId>com.github.vatbub</groupId>
  <artifactId>mslinks</artifactId>
  <version>1.0.5</version>
</dependency>

Refer answer by https://stackoverflow.com/a/38952899/4697928

private final String STARTUP_PATH = "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup";

private void checkAutoStartPresent() {
    File file = new File(System.getProperty("user.home") + STARTUP_PATH + "/YourShortcutFileName.lnk");
    if (!file.exists()) { // shortcut not found
        try {
            ShellLink.createLink("/YourTargetProgramPath.exe", file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Abishek Stephen
  • 386
  • 1
  • 3
  • 15