2

I need to register the file association for a certain file type - in fact, I just need to launch a certain Java program with certain arguments and a name of that file.

I got as far as the following:

// in fff-assoc.cmd file:
assoc .fff=SomeFile
ftype SomeFile=java -jar some.jar <arguments1> "%%1" <arguments2>

It works properly for ASCII file names. But when I try to double-click some file with non-ASCII symbols in name, the argument passed looks like "????" (int value of each char = 63).

How can I fix those associations?

Rogach
  • 26,050
  • 21
  • 93
  • 172

3 Answers3

2

If what bobince says is accurate and you cannot reliably get the data to java directly, one alternative solution would be to write a small "shim" program in another language (e.g. C, C++ or C#).

The idea is that the program grabs the input as UNICODE, encodes it so that it's expressible using only ASCII characters (e.g. by using base64, or even something as simple as encoding every character as its numerical equivalent) and then assembles the command line argument to use and launches java itself using CreateProcess.

Your Java code could "undo" the encoding, reconstructing the UNICODE name and proceeding to use it. It's a bit of a roundabout way and requires an extra component for your software, but it should work around the restriction detailed above, if indeed that is an actual restriction.

Update: This is the basic code for the shim program. It encodes input as a sequence of integers, separated by colons. It doesn't do much in the way of error checking and you might want to improve it slightly, but it should at least get you started and going in the right direction.

You should grab Visual Studio Express (if you don't already have Visual Studio) and create a new Visual C++ project, choose "Win32" and select "Win32 Project". Choose "Win32 application". After the project is created, replace everything in the .cpp file that is displayed with this code:

#include "stdafx.h"
#include <string>

int APIENTRY _tWinMain(HINSTANCE, HINSTANCE, LPTSTR lpCmdLine, int)
{
    std::string filename;

    while((lpCmdLine != NULL) && (*lpCmdLine != 0))
    {
        if(filename.length() != 0)
            filename.append(":");

        char buf[32];

        sprintf(buf, "%u", (unsigned int)(*lpCmdLine++));

        filename.append(buf);   
    }

    if(filename.length() == 0)
        return 0;

    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof(PROCESS_INFORMATION));

    STARTUPINFOA si;
    memset(&si, 0, sizeof(STARTUPINFOA));
    si.cb = sizeof(STARTUPINFOA);

    char *buf = new char[filename.length() + 256]; // ensure that 256 is enough for your extra arguments!

    sprintf(buf, "java.exe -jar some.jar <arguments1> \"%s\" <arguments2>", filename.c_str());

    // CHECKME: You hard-coded the path for java.exe here. While that may work on your system
    // is it guaranteed that it will work on every system?

    if(CreateProcessA("C:\\Program Files\\Java\\jre7\\bin\\java.exe", buf, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
    {
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }

    delete[] buf;

    return 0;
}

You should be able to figure the details on how to compile and so on fairly easily.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • Yes, that would probably work. Can you give a small example of such program - not the transformation code (I'll probably figure that out myself), but the packaging details - for example, how I should compile it, and should I package it into `.exe` (I'm not fluent in neither C nor windows apis)? – Rogach Nov 24 '12 at 10:34
  • I'm typing this on an iPhone so I can't easily give you a sample now, but I will post a small "framework" application that does this tomorrow. – Nik Bougalis Nov 24 '12 at 10:38
  • I grabbed VSE 2010, created the project, pasted the code and stuff, but the running part really confused me. If I change configuration to "Release" and hit F7 (Build Solution), I get `Program.exe` in the `Release` directory. But when I try to run it (from command line, program.exe), I get absolutely nothing. Even when I include `std::cout << "Hello\n";` right before `filename` declaration. – Rogach Nov 24 '12 at 15:01
  • Ah, it's not a console project. – Rogach Nov 24 '12 at 15:11
  • I created a sample java jar, that does nothing apart from printing the argument to stdout and to file in user home directory. In printf, I have `printf(buf, "java.exe -jar X:\\Tor\\uc\\uc.jar \"%s\" ", filename.c_str());`, in CreateProcessA - `"C:\\Program Files\\Java\\jre7\\bin\\"`, and still, nothing happens when I execute the `program.exe` - no output, no files. – Rogach Nov 24 '12 at 15:37
  • Ah, I forgot to include "java.exe" in the path, since I was misleaded by "java.exe" in command line. Now I see console window flickering, with java cli help - indicating that something is wrong. But I can't see that error, because console closes immediately. – Rogach Nov 24 '12 at 15:55
  • Strange thing - even if I set contents of that string to "java.exe -jersion", it still prints only the help output. Seems that it fails to provide command line to the executable (since java.exe only prints help output if there are no arguments) – Rogach Nov 24 '12 at 16:06
  • Yes, there's something wrong with printf to that buf - giving a string directly to CreateProcessA does the job. – Rogach Nov 24 '12 at 16:09
  • Finally! There was a typo - last `printf` should be `sprintf`. – Rogach Nov 24 '12 at 16:11
  • After all the suffering, I made my application and this hack work together. Thank you! Your answer was of tremendous help to me. I'll edit it a bit, to correct the errors, and will accept it. – Rogach Nov 24 '12 at 16:40
  • SO says that bounty can be only awarded in 15 hours :) So I'll have to wait a bit. – Rogach Nov 24 '12 at 16:42
  • Great news! Good job on getting it to work; I know that C/C++ are a little out of your comfort zone but that's not always a bad thing ;) – Nik Bougalis Nov 24 '12 at 17:58
  • I did a small tweak to the code - delete[] the buffer that we dynamically allocated. It's not really a problem, since the memory is returned anyways when the program exits, but it's good to be thorough about memory management. One more thing, in your edit you hard-coded the path to CreateProcessA - is it *guaranteed* that the path to java.exe is what you hardcoded? If not, you will need to retrieve the path dynamically to ensure that this works on every computer. – Nik Bougalis Nov 24 '12 at 18:35
  • Good point - but don't know a way of dynamically finding java.exe. Is there some good way? I think that it is almost guaranteed that it would be at that exact location on machines where I'll be deploying, but dynamically finding it would be more fool-proof. – Rogach Nov 24 '12 at 20:16
1

I just need to launch a certain Java program with certain arguments and a name of that file.

Unfortunately this 'just' is not actually possible, due to the MS implementation of the standard C library that Java uses to receive argument input (amongst other things). Unless you go straight to the native Win32 API, bypassing standard Java or C interfaces,

See this question for background.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
  • Do I understand it right - it is completely impossible to use file associations + java (or javaws) with non-ascii file names? I do not insist on using command line for that, I would be completely satisfied with any other solution. – Rogach Nov 22 '12 at 15:34
  • It's more about command line arguments in general than specifically file associations. However, looking at your example, I see you're using `%1` - normally that will pass the DOS-style 8.3 filename on the command line, which should dodge the issue (as opposed to `%L` which gives you the full, long/Unicode name). Have 8.3 filenames been disabled on the machine or something? Or are the non-ASCII characters in some of the other arguments than the filename? – bobince Nov 22 '12 at 15:50
  • No, there are no other non-ASCII arguments. As far as I recall, if I would run it from .bat file, then DOS-style names would be passed - but switching to .cmd made it pass proper names. – Rogach Nov 22 '12 at 15:52
  • Switching the way you ran the `ftype` command made a difference? This shouldn't happen - both formats should handle percent in the same way as far as I'm aware. Check in the registry under `HKEY_CLASSES_ROOT\Somefile\Shell\Open\Command` to see what (if any) difference calling `ftype` via `.bat` or `.cmd` made. – bobince Nov 22 '12 at 16:28
  • There is one thing, that bugs me - if, for example, I launch not a java program with that argument, but `iexplore.exe` (IE 7) with that argument, it displays correct name in address bar, even if it is non-ascii. So there is probably some non-obvious way to launch an app with proper arguments. – Rogach Nov 23 '12 at 05:24
1

When calling java from the command line, you can specify the encoding of the parameters (which will be used to create the strings in args[]):

java -jar -Dsun.jnu.encoding=cp1252 yourFileName

When using non-ASCII characters, the specified charset has an impact on the value of args[0]. Not sure if that would apply to file associations though.

Note: I'm not sure what other uses that parameter has - this post seems to say none.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I'm not sure about plain java process, but it doesn't work through javaws+associations, for sure - it just hangs the process. – Rogach Nov 24 '12 at 10:37
  • You did not mention javaws. You can pass in arguments in your jnlp too (not sure if that one is allowed for a non-signed jar though) - for example: ``. – assylias Nov 24 '12 at 10:43
  • I tried to do it with `javaws "-JDsun.jnu.encoding=cp1252" ...`. – Rogach Nov 24 '12 at 10:48
  • I think it would be `javaws -J-Dsun.jnu.encoding=cp1252` – assylias Nov 24 '12 at 10:55
  • 1
    Yes, you are right. But that didn't help - it still fails to recognize the arguments. – Rogach Nov 24 '12 at 14:57
  • You might find this no so related discussion interesting: http://stackoverflow.com/questions/13348811/get-list-of-processes-on-windows-in-a-charset-safe-way – assylias Nov 24 '12 at 15:38