0

On Windows 10, I have a shortcut file in the "SendTo" directory. It is a shortcut to a .bat file.

Inside the .bat file can have just the command "python <filepath> %*" or "java -jar <filepath> %*".

When I select and right click file(s) from Windows Explorer and have it sent to this shortcut file, it will run the program from <filepath> with the selected file(s) as arguments.

I am trying to send files with filenames containing Japanese characters as arguments. The filenames are passed to python programs just fine, but for Java programs, the args for the filenames are messed up and the Java program cannot find the file.

For example, in Java and with locale of Japan, a filename of Filename ファイル名.txt becomes Filename 繝輔ぃ繧、繝ォ蜷�.txt in the args. Other locales also do not work. The result is the same if I send the args to python and then from python to Java.

How to make it so Java gets the proper filename or can find the file properly?

Questionerer
  • 91
  • 1
  • 11
  • 1
    Why do you use a batch file interpreted by Windows command processor `cmd.exe` which does not really support Unicode file names names? Why do you not create a shortcut file in __Send to__ folder which runs `python.exe` (with full path) and the file name passed by Windows Explorer? Why do you not used a shortcut file which runs `java.exe` (with full path) and the file name passed by Windows Explorer? – Mofi Jan 26 '21 at 11:14
  • 1
    If you really want to use a script to start `python.exe` __or__ `java.exe` with the appropriate arguments passed from Windows Explorer to the script interpreter, I recommend to use a PowerShell script and configure in properties of the shortcut file in __Send to__ folder to run PowerShell.exe (with full path) with the PowerShell script file name (with full path) and the file name(s) passed by Windows Explorer to PowerShell.exe and passed further to the PowerShell script for passing them further to `python.exe` __or__ `java.exe`. PowerShell is full Unicode aware. – Mofi Jan 26 '21 at 11:16
  • I used batch file since it was only thing I knew and it worked for python all the time just not Java. How to do the "shortcut file which runs java.exe (with full path) and the file name passed by Windows Explorer?"? – Questionerer Jan 27 '21 at 00:04
  • 1
    Open a [command prompt](https://www.howtogeek.com/235101/), run `where java.exe /?` and look on output of `java.exe` with full path. Then browse in Windows File Explorer to `java.exe` in output folder. Right click on `java.exe` and click in context submenu __Send to__ on item __Desktop (create shortcut)__. On your desktop is now one more shortcut file with name `java.exe.lnk` whereby the file extension `.lnk` is not displayed. Right click on this shortcut file and left click on __Rename__ and change the file name to whatever is suitable for you. The file extension `.lnk` is kept. – Mofi Jan 27 '21 at 07:16
  • 1
    Right click once again on the shortcut file on desktop and click in context menu on last item __Properties__. There must be modified the property __Target__ on which a space and next `-jar ` must be appended as used in the batch file. You can modify also other properties like entering a comment shown as tooltip on hovering the mouse pointer over the shortcut file, or changing font and font size for the console window, the window layout and colors, etc. Click on __OK__. Then cut the shortcut file, browse to the folder `%APPDATA%\Microsoft\Windows\SendTo` and paste it there. – Mofi Jan 27 '21 at 07:21
  • 1
    Copy and paste the string `%APPDATA%\Microsoft\Windows\SendTo` into the address bar of Windows File Explorer and press key RETURN or ENTER to open this folder. Now you have in context submenu __Send to__ one more item with the name of the shortcut file and on clicking on this menu item, `explorer.exe` runs `java.exe` with the additional arguments defined on __Target__ and all file/folder names currently selected with full path appended as additional arguments respectively the file/folder on which the right click was done on no file/folder selected. – Mofi Jan 27 '21 at 07:24
  • I have followed the steps but the explorer.exe seems to give same result as batch file. The java args still are messed up filename. However, I have solved my problem by sending the file paths of selected files through batch file to python and then have python write out the names into a text file. Then have the python program run the java program and get it to read the file paths from the text file. – Questionerer Jan 27 '21 at 08:49
  • 1
    Are you sure that Windows File Explorer passes the file names wrong to `java.exe`? It could be that the Java application itself does not support file names in Unicode. I recommend to download, extract and run as administrator Sysinternals (Microsoft) [Process Monitor](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon). In the log created double click on `java.exe` and on tab __Process__ look on __Command Line__. – Mofi Jan 27 '21 at 09:06
  • The java program can read the Japanese characters just fine if they are not sent through args. It only gets messed up at the args stage. – Questionerer Jan 27 '21 at 09:09
  • Real filename appears just fine in the Process monitor: "D:\Filename ファイル名.txt". The file is stored in D drive. – Questionerer Jan 27 '21 at 09:59
  • 1
    Well, this means Windows File Explorer passes the file names correct to `java.exe`. So either `java.exe` does not pass the file name arguments correct to the executed Java application or the Java application does not correct process the arguments passed to it. I suggest to look on [Passing command line Unicode argument to Java code](https://stackoverflow.com/questions/7660651/) and similar questions. I am not a Java programming expert. – Mofi Jan 27 '21 at 10:06
  • 1
    Please note that in your case `java.exe` is started directly by `explorer.exe` without using `cmd.exe` using [CreateProcessW](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) and [STARTUPINFOW](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow). So all answers related to locale code page used by `cmd` and displayed on running `chcp` in a command prompt window are irrelevant. There is no `cmd.exe` used in your case now with the shortcut file. – Mofi Jan 27 '21 at 10:10
  • I see. The problem is in the `public static void main(String[] args)` method of Java where Java seems to fail to properly get the arguments. – Questionerer Jan 27 '21 at 11:26

1 Answers1

1

You are encountering an unresolved issue with Java. See open bug JDK-8124977 cmdline encoding challenges on Windows which consolidates several problems related to passing Unicode arguments to a Java application from the command line.

Java 18 (to be released next month) resolves some UTF-8 issues with the implementation of JEP 400: UTF-8 by Default, but specifically not your problem unfortunately. From the "Goals" for JEP400:

  1. Standardize on UTF-8 throughout the standard Java APIs, except for console I/O. [Emphasis mine]

However, there is a workaround. See Netbeans Chinese characters in java project properties run arguments, and in particular this answer which successfully processes Chinese characters passed as command line arguments using JNA (Java Native Access). From that answer:

JNA allows you to invoke Windows API methods from Java, without using native code. So in your Java application you can call Win API methods such as GetCommandLineW() and CommandLineToArgvW() directly, to access details about the command line used to invoke your program, including any arguments passed. Both of those methods support Unicode.

So the code in that answer does not read the arguments passed to main() directly. Instead it uses JNA to invoke the Win API methods to access them.

While that code was processing Chinese characters passed as arguments from the command line, it would work just as well for Japanese characters, including your Japanese filenames.

skomisa
  • 16,436
  • 7
  • 61
  • 102