1

My app launches and loads the file if I do: myApp /file:c:\nospaces.asd from cmd but if I do myApp /file:c:\with spaces.asd it won't work because the program receives two arguments: myApp /file:c:\with and spaces.asd.

I know that I can do myApp "/file:c:\with spaces.asd" and it'll work like that from cmd. However this isn't a good solution because if I double click the .asd file (custom extension) (and select launch with my app) then main won't be getting the arguments as one line but as two arguments.

How can I go about fixing this issue so that my main will receive only one argument when double clicking the file?

Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • Remove the spaces from your app filename – FirebladeDan Aug 24 '15 at 00:41
  • @dasblinkenlight yes but I can only do that from a command prompt, if the user jsut double clicks the file, then that won't work. – Aequitas Aug 24 '15 at 00:43
  • @FirebladeDan These files can be named by the user and so it could be called anything, spaces should be allowed. Like a microsoft word file, you wouldn't expect it to fail if you saved it with a space in the name – Aequitas Aug 24 '15 at 00:43
  • 1
    You may need to modify the registry to make sure command line args are wrapped with quotes. These answers might be helpful: http://superuser.com/questions/361816/pass-command-line-arguments-to-windows-open-with – Marc Baumbach Aug 24 '15 at 00:45
  • Well I would just make my life easy and make spaces illegal in the file creation. Sounds like you've spent enough time on this. – FirebladeDan Aug 24 '15 at 00:53
  • Why does double-clicking on the .asd file launch myApp? How did you set up the association? – VGR Aug 24 '15 at 00:55
  • @VGR through windows, if you double click an unknown type it'll ask you what program to open with and then I can select my exe from there – Aequitas Aug 24 '15 at 01:02
  • And what is that .exe file? Is it javaw.exe, or did you use some utility to turn your Java application into a self-contained .exe? – VGR Aug 24 '15 at 01:06
  • @MarcBaumbach that link is useful and I think i've got it working now, thanks! – Aequitas Aug 24 '15 at 01:15

2 Answers2

1

You could join the arguments together if the file is not found. Something like this:

public static void main(String[] args) {
    String fileName = joinArgumentsToValidFileName(args);
}

public static String joinArgumentsToValidFileName(String[] args) {
    if(args.length == 0) {
        return "";
    }
    String fileName = args[0];
    int index = 1;
    while(!new File(fileName).exists() && index < args.length) {
        fileName += " " + args[index];
        index++;
    }
    return fileName;
}

This assumes that the first argument (or the arguments) must be the file name. Any additional agruments could be evaluated by remembering the offset index somehow (not included in the code above).

But note: This is a not standard behavior of passing arguments to an application and could lead to cunfusion. So if you find a way to pass a "" wrapped file name argument, don't do this!

Mathias Begert
  • 2,354
  • 1
  • 16
  • 26
0

Double clicking to launch java would be affected by whats registered in windows related to the same i.e windows registry for jar-file launch command.

These are some similar questions asked on SO you can take a look at:

Running JAR file on Windows

java can run jar from cmd but not by double clicking

They mention the useful part which is ensuring you have the wildcard behind the executable in the registry c:\...\javaw.exe" -jar "%1" %

Community
  • 1
  • 1