4

I have a windows desktop application written on Java. I have a checkbox there saying "Launch at system startup". So if this checkbox is checked then I want the app to start when a user logs in to Windows. And if it's not checked then I want to remove it (If it already exists).

And I want to do it from my application using Java (I know there are some other methods like batch file and windows service).

I have checked Stack Overflow code but it didn't work. Actually I just want a solution like Code Project.

But unfortunately it's in C# .net. So how can I achieve this using Java?

EDIT: I am also open for JNA/JNI approach. The thing is I just need to do it in Java. And it doesn't matter whatever I am using. I am ready to go for JNA/JNI.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Partho Biswas
  • 2,290
  • 1
  • 24
  • 39
  • 2
    This is actually some what difficult, as the creation of the shortcut is actually done via COM, yeah. You can delete the short relatively easily, if you know the "startup" menu exists or do it via the registry, again, not something you can do from within Java without some JNA/JNI support. Personally I make use of [TeamDev's JNIWrapper and WinPack APIs](http://www.teamdev.com/jniwrapper), but they are commercial products – MadProgrammer Jun 05 '15 at 05:09
  • @MadProgrammer : Thanks for your information. I am also open for JNA/JNI approach. The thing is i just need to do it in Java. And it doesn't matter whatever i am using. I am ready to go for JNA/JNI. Can you please show me some path ? – Partho Biswas Jun 05 '15 at 05:39
  • 1
    Well, you could start with [Read/Write Windows Registry using JNA](http://www.rgagnon.com/javadetails/java-read-write-windows-registry-using-jna.html), this will allow you to write to the "startup" registry entry, which is probably one of the simper ways to do this (as it's easier to find then the startup menu) – MadProgrammer Jun 05 '15 at 05:41
  • 1
    You'd have to verify it's support in the latest JNA release, but you might be able to use something like [this](https://groups.google.com/forum/#!topic/jna-users/Zgkx7XQAJYc) to create a shortcut – MadProgrammer Jun 05 '15 at 05:44
  • @MadProgrammer : Thanks for the link. I will definitely give a try with it. Will update the result here. – Partho Biswas Jun 05 '15 at 06:06
  • @ParthoBiswas: Any luck yet? – HELOX Jun 09 '15 at 07:00

1 Answers1

3

Another option which is much simpeler than a COM solution is to add/remove a file to one of the Windows startup folders. You could do something like this:

    String allUsersStartupFolder = "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Startup";
    //String personalStartupFolder = "C:/Users/" + System.getProperty("user.name") + "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup";

    File startupFolder = new File(allUsersStartupFolder);
    System.out.println(startupFolder);
    File startupFile = new File(startupFolder, "MyProgramStartup.bat");

    if(startupFile.exists()){
        System.out.println("Unregister");
        startupFile.delete();
    }else{
        System.out.println("Register");
        Files.write(startupFile.toPath(), "java -jar MyProgram.jar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
HELOX
  • 529
  • 2
  • 13