7

I have written a registry that creates .myext.

Double-clicking on my file.myext refers to my executable file (converted from a batch file that opens a .jar) which then opens my notepad application.


The registry

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.myext]
@="myext"

[HKEY_CLASSES_ROOT\.myext\ShellNew]

[HKEY_CLASSES_ROOT\myext]
@=".myext file"
"EditFlags"=dword:00000000
"BrowserFlags"=dword:00000008

[HKEY_CLASSES_ROOT\myext\DefaultIcon]
@="C:\\...\\icon.ico, 0"

[HKEY_CLASSES_ROOT\myext\shell]

[HKEY_CLASSES_ROOT\myext\shell\Open]

[HKEY_CLASSES_ROOT\myext\shell\Open\command]
@="C:\\...\\run.exe %1"

My executable (run.exe) that was converted from a batch

start /min "C:\...\javaw.exe -jar" "C:\...\mjar.jar"

Problem?

I don't know why I am receiving that error message. Perhaps it was this conversion application that's causing some problems.


SUGGESTIONS

Here is what I have done after everyone's suggestions. I am able to successfully open my text editor (with any one of the following suggestions) after clicking on a document, but no text appears in the JTextPane. If I choose to open the document in Windows Notepad, all the text shows up.

P.S. I am not using DDE and I no longer receive the error message: "file.myext" is not a valid Win32 application.

BATCH FILE SUGGESTIONS

start "Mike's Text Editor" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar"

start /min "C:\...\javaw.exe -jar" "C:\...\mjar.jar" "%1"

REGISTRY SUGGESTION

[HKEY_CLASSES_ROOT\myext\shell\open\command]
@="\"C:\\...\\run.exe\" \"%1\""
ᔕᖺᘎᕊ
  • 6,173
  • 4
  • 33
  • 45
Rob
  • 656
  • 1
  • 8
  • 20
  • Do you *need* it to be an executable instead of a batch file? Did you try running the executable on its own? Have you tried using the batch file in the `command` key to see if that works? – Synetech Feb 15 '12 at 20:54
  • @Synetech Yes. The executable works on its own and it works using the batch file in the `command` key. The error message pops up when I double click on my file that was saved using my Java application. – Rob Feb 15 '12 at 21:14
  • So the problem is with the `.JAR` file? Or is it with your Java installation? – Synetech Feb 16 '12 at 01:00
  • @Synetech There's nothing wrong with either of them. I can open and save files when I have the application open. There might be an issue with the registry... other than that I'm not too sure. – Rob Feb 16 '12 at 01:43
  • Okay, so to summarize; the registry entry (and custom file-type) work correctly when using the batch file as the command to run and the executable works when run manually? It seems that you already specified the file to be passed to Java in the batch file; is there a reason for the `%1` in the command? (To double-check your registry entries, try creating a plain-text file, save it as something like `foobar.myext`, then replacing the command with `notepad` to see if double-clicking the `.myext` file will open it in Notepad.) – Synetech Feb 16 '12 at 02:10
  • If you post run.exe somewhere, we could have a look at it. – harrymc Feb 16 '12 at 07:54
  • 1
    Forgot to ask this, how does your `run.exe` *use* the `file.myext` or whatever file is associated with it? I don't find any `%1` in your batch script, which is necessary to pass in arguments (`file.myext` in this case). – ADTC Feb 19 '12 at 07:40
  • @Synetech and @ADTC Sorry for not responding sooner, I've been bogged down with school work the past couple of days. The only thing that I needed to do was remove the `%1`... that's it! But that brought up a whole new bunch of questions because my notepad application doesn't display any text after double clicking on a document (whereas opening up the application, clicking open and choosing it will display all the characters)... which I will probably post a question about it at a later date. Thank you very much for your help! I will continue looking at the answers and award the bounty soon. – Rob Feb 19 '12 at 14:43
  • @Mike Notepad is not opening the file (and not displaying the text) *because* you removed the `%1`!!! The `%1` is a very important component of `[HKEY_CLASSES_ROOT\myext\shell\Open\command]` because it passes in the path of the file that you're double-clicking. You removed it, so now Notepad is unable to know what file you're double-clicking on. Hence Notepad just opens an *Untitled* window. (This is precisely why I asked why your batch file does not use `%1` anywhere. File association is used to open the associated file in a program, not just run it. You can just create shortcuts for that.) – ADTC Feb 19 '12 at 15:53
  • Try compiling and testing with a batch file that simply prints the name of the file (and maybe dumps it): `echo %1 & more %1` – Synetech Feb 20 '12 at 18:27
  • 1
    So does `start "" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar" %1` work? It *should*. – Synetech Feb 23 '12 at 03:21
  • 1
    To be honest, it’s not really clear what’s going on. What exactly are the `.myext` files and what exactly is `mjar.jar`? Is `mjar.jar` a tet editor and `.myext` files are text files? Are you trying to create a new text filetype that is opened with a Java text editor? – Synetech Feb 23 '12 at 03:23
  • @Synetech Yes `mjar.jar` is a text editor and `.myext` are text files. And yes, I am trying to create a new text file type that opens up with my text editor created with Java. The command `start "" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar" %1` opens up the application after double clicking on a `.myext` file, but no text appears. – Rob Feb 23 '12 at 12:25
  • @Synetech I really don't know what else to say. This stuff is above my level of expertise... which isn't that high in the first place. I'm just happy that the error message went away and the application is opening up (regardless if the text doesn't show up). So in that regard, my question was answered. – Rob Feb 23 '12 at 13:16
  • Then based on your last comment, I can only wonder if the Java program is correctly reading/opening the specified file. But you said that manually running it from the command-line (eg `C:\...> javaw -jar mjar.jar blah.myext`) *does* work correctly right? Double-check the source for the `.jar` file to make sure that it is correctly copying (and checking) [`args[0]`](http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html) to a variable and then using that everywhere to access (ie open, read, close) the file. – Synetech Feb 23 '12 at 17:53
  • @Mike `start "" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar" %1` It should be `start "" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar" "%1"`. You'll have to enclose the %1 in quotes so that file paths with spaces get passed in correctly. – ADTC Feb 24 '12 at 09:06
  • @Mike I hope to see you soon in the [chat room](http://chat.stackexchange.com/rooms/2572/file-myext-is-not-a-valid-win32-application). We'll need to discuss about your Java code and also I want to guide you in further testing and narrow down to the source of problem. From everything you said, I have identified two different problem sources, but I'll need you to do some tests before I strike out one and home in on the other. – ADTC Feb 24 '12 at 09:23
  • If all else fails, you could try a [different batch-file compiler](http://www.robvanderwoude.com/scriptcompilers.php). – Synetech Feb 25 '12 at 02:47
  • @Synetech the batch compiler he used has no problem. I have tested it and it works perfect for the purpose he intends. The problem is in his Java program itself. Mike, I have posted some updates in the chat. – ADTC Feb 25 '12 at 12:06
  • @ADTC, well that’s strange because he has said that the batch file and Java program work from the command-line, just not from the context-menu. I guess the definition of “works” was not as obvious as I thought. – Synetech Feb 25 '12 at 18:57
  • @Synetech, well it turns out the Java program was *not* working as intended. He had to modify the code. See the chat discussion. He just told me in the chat that the modified code works now. – ADTC Feb 25 '12 at 22:26
  • Yes, I saw. He has an account on SO, so he can get help there too. (I’m still confused as to why he said it worked if it didn’t.) – Synetech Feb 25 '12 at 22:29
  • @Synetech I'm really sorry about all of the confusion but everything is working properly now. I misunderstood some things that you guys were saying, making it difficult for you guys to pinpoint the problem. – Rob Feb 25 '12 at 23:14

3 Answers3

4

Your problem is how you use the start command.

By putting the first parameter into "" you assign that as the title of the window for the started program. Then you pass %1 into it (which you noted in the comments of your question). %1 is the filename of the .myext file you clicked. So that is the file start tries to execute.

Which results in the error you're seeing.

So, to solve it, just use start like this:

start "something" /min "C:\...\javaw.exe" -jar "C:\...\mjar.jar"

The first parameter passed to start which is enclosed in "" is expected to be the title of the resulting (console) window.

The second parameter (that doesn't start with a /) is expected to be the executable to start. If the path to it contains spaces, it needs to be enclosed in "". Otherwise, they're optional.

The third and all following parameters will be passed to the executable.
So those don't need to be enclosed in "" separately. But you do need to enclose paths (that contain spaces) in those parameters in "" so the executable can parse them properly when started.

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
  • 1
    +1, sounds very likely. This has bitten me at least twice, and still I didn't spot it in the OP... – Jonas Heidelberg Feb 22 '12 at 19:26
  • I tried your suggestion and I received an error message: `Windows cannot find 'C:\...\javaw.exe -jar'. Make sure you typed the name correctly, and then try again.` Just to make sure, `"something"` is the title of the window, right? – Rob Feb 22 '12 at 20:55
  • Yes, "something" the title of the window. I think there might be an incorrect placed `"` in my answer. Will fix it right now. – Oliver Salzburg Feb 22 '12 at 21:06
  • The application opens up properly but the text area is still blank. P.S. the batch file does not use DDE. – Rob Feb 22 '12 at 23:05
  • 1
    Please put the exact contents of your batch file in your question. I also don't know about the text area. Please put all relevant information from comments in your question. – Oliver Salzburg Feb 22 '12 at 23:09
  • I have updated my question based on your requests. Please refer to the section labeled **SUGGESTIONS**. – Rob Feb 22 '12 at 23:44
  • @Jonas, agreed. This was the bane of my existence for a long time until I learned about it, and even then, I kept forgetting about it until not that long ago. (I am fairly sure that it was because it did not apply in older versions.) – Synetech Feb 23 '12 at 03:15
  • The points I made in my answer are, from my point of view, all correct. Parameters that should be passed on *to the .jar file*, should be placed on the command line after the path to the jar (possibly in `""`). You should be able to debug this easily from the command line. You're the only one with that `.jar`, that makes it a bit difficult ;) – Oliver Salzburg Feb 23 '12 at 10:14
2

Problem

You have your command set to this:

@="C:\\...\\run.exe %1"

Spaces are delimiters and used to separate different parts of a command and its arguments. If you had no spaces in the filenames, then it would work:

C:\Foobar\run.exe c:\test.myext

If have spaces in the file (or its path) being passed, the program may or may work correctly depending on how it interprets its arguments:

C:\Foobar\run.exe C:\My Docs\test.myext /switch

Is C:\My argument one and Docs\test.myext argument two? Are all the arguments a single string? Is there some special interpretation?

If you have spaces in your program (or its path), then Windows will not be able to identify which parts are the path and/or filename, and which parts are other arguments:

C:\Program Files\Foobar\run.exe C:\test.myext

Windows tries to run the file C:\Program and pass Files\Foobar\run.exe and C:\test.myext as arguments to it.

As you can see, this is clearly not a valid filename. If you entered it in the Run dialog, you would get the same cannot find/not valid app error message.


Solution

What you need to do is to wrap file/pathnames in quotation marks to clarify that they are a single unit. This is usually straight-forward, but not always.

In your case, it should be pretty easy. If you are entering it directly in Regedit, use this (replacing the paths as necessary). Navigate to HKCR\myext\shell\open\command and edit the default item and set it to:

"C:\Program Files\Foobar\run.exe" "%1"

If you are putting it in a .REG file, use this:

[HKEY_CLASSES_ROOT\myext\shell\open\command]
@="\"C:\\Program Files\\Foobar\\run.exe\" \"%1\""

Note how the slashes and quotes are escaped (with a slash), but both the program and the file argument are quoted. This is good regardless of whether there are spaces in the path/filename; it’s just safer.

Also, you can replace the %1 with %L to pass the fully-qualified path to the file should your program require it.

Synetech
  • 68,243
  • 36
  • 223
  • 356
  • I want to apologize for not getting back to you sooner but I would also like to thank you for the very knowledgeable information. I have updated all of my code and compared it with ADTC and your suggestions, but I continue to have problems with showing the text in my application (no problems with opening the file with Windows Notepad). – Rob Feb 22 '12 at 00:03
  • It narrows the problem down to the compiled `EXE`… sort of. From your responses it seems that you can successfully use an executable like Notepad and successfully use your raw batch file. The problem only arises when you use the compiled batch file. (You did say that the compiled batch file runs correctly when you manually run it right?) That makes it quite confusing. If the executable is not corrupt and does indeed work when run manually, and the context-menu item you created works with other programs, then why would the compiled batch file not work from the menu? It doesn’t use DDE does it? – Synetech Feb 22 '12 at 01:55
  • @Synetech //why would the compiled batch file not work from the menu? It doesn’t use DDE does it?// In my test, this part worked absolutely fine. I could execute the context-menu item and the compiled `EXE` file will correctly get and display the file name. (I'm pretty sure it would display the file contents as well if I had included a `more` command.) Until @Mike confirms that his `.jar` file is able to **correctly process** the incoming file, we can't narrow down the problem source properly. (He has not done that in his comment here and has not answered my question yet!) – ADTC Feb 22 '12 at 05:00
  • @Synetech and Mike [Please join the chat room I created for this topic.](http://chat.stackexchange.com/rooms/2572/file-myext-is-not-a-valid-win32-application) Thanks! This discussion using comments here and there is getting very long and disorganized, and the wait times are very tiring. – ADTC Feb 22 '12 at 05:10
1

New Answer

This is the bare minimum you need to associate correctly. I found this out by trying my own tip number 4 below (You can find out how Windows does this for you...).

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.myext]
@="myext"

[HKEY_CLASSES_ROOT\myext]
@=".myext file"

[HKEY_CLASSES_ROOT\myext\shell]

[HKEY_CLASSES_ROOT\myext\shell\open]

[HKEY_CLASSES_ROOT\myext\shell\open\command]
@="\"C:\\...\\run.exe\" \"%1\""

You can add DefaultIcon and other things as necessary, but without DefaultIcon, Windows will simply use the icon in run.exe (if it has one. You can add one in the Batch To Exe Converter when you create the run.exe).

Importance of %1 and quote marks

The reason why you need to put a %1 there is to pass the path and name of the associated file (file.myext) into the program run.exe. Without passing this in, your association is pointless as it's functioning simply as a shortcut. This is unnecessary as you can simply create a normal shortcut to run.exe to serve the same purpose.

Your batch file should also have a %1 somewhere inside so that the path and name of the associated file (file.myext) is used inside the batch file (presumably to pass it to your mjar.jar which will do something with the file). Otherwise, no matter which associated file you double-click on, you'll always get the same result from your Java program. This is once again pointless as you can simply have a shortcut to the batch file to serve the same purpose.

It is also important to enclose the %1 in quotation marks, as file paths can contain spaces, and without the quotes (") these spaces can split the path into two or more arguments (when the entire path is intended to be regarded as one argument).

Example batch file

Here is the batch file I converted to exe for testing. It simply displays whatever the value of %1 is.

@echo %1
@pause

Your batch file could be as shown below (so that mjar.jar can get the path and name of the file you're double-clicking on):

start /min "C:\...\javaw.exe -jar" "C:\...\mjar.jar" "%1"

I associated the exe file with .myext extension (using method in my tip 4) and then checked registry to create the above .reg file. When I double-click on a file with .myext extension, a command window opens, displaying (echo command) the path and name of the file I double-clicked (this is how my test batch file is using the associated file).

Java program

(This is a summary of the full chat discussion that eventually solved your problem.) Your Java program contained in mjar.jar must be prepared to accept the incoming argument and use it. Your intention is for your program to automatically open the file referred by the incoming argument and display its contents. Hence the main method should be something like this:

public static void main (String[] args) {
    if (args.length >= 1) {
        openFile(args[0]); 
    }
}

The openFile method is a method that opens the file by the name passed into it. The if statement ensures that args[0] is only read when there is such an argument (avoiding ArrayIndexOutOfBoundsException). Only the first argument args[0] is used in the above code; all other arguments (args[1], args[2], etc.) are ignored. The openFile method would be something like this (descriptors and return types not included):

openFile(String filename) {
    // code here to open the file referred by "filename" variable,
    // read its contents and display it on the GUI
    // or use it in the program as intended
}

If your program has an Open command built into its GUI, after the user chooses a file with this command, your application can make use of the same openFile method above to open the chosen file and display its contents.

Previous Answer

I do not have a definitive answer to your problem yet, but here are some tips to get you started:

  • Have you tried adding quotes? Like this: @="\"C:\\...\\run.exe\" \"%1\""
    In the registry, the (Default) value will show up like this: "C:\...\run.exe" "%1"

  • Read Microsoft's official MSDN doc about File Type Association. You will also have to read up about Programmatic Identifiers (linked in the first para of that document).

  • Try associating your .myext file type with Notepad first. Find out how Notepad is associated to .txt files and follow the example. If done correctly, Notepad should open your file.myext file.

  • You can find out how Windows does this for you. Right-click file.myext, click Open with > Choose default program...^ and Browse to find your run.exe file. Associate and open, then investigate the Windows registry to find out how Windows stored your manual association. You can then simply export the file type and the programmatic identifier to reg files.

^ If file.myext is unassociated, click Open > Select a program from a list of installed programs.

PS1: Apparently, you must have double backslash in .reg files.
PS2: It's better to directly edit stuff in registry, test the effects, then export the keys to .reg files and combine them to a single file, rather than create a .reg file yourself.

ADTC
  • 2,954
  • 3
  • 30
  • 49
  • Excellent answer. I tried adding quotes in numerous places (just like your suggestion) but it never worked properly. Thank you very much for the link! I've been reading up on it and it seems quite helpful. – Rob Feb 19 '12 at 14:49
  • @Mike I have updated the answer. Please check for new answer above previous one. – ADTC Feb 19 '12 at 16:34
  • Thank you for the explanation on why it is important to have %1, I had a hunch that it was used for referencing or passing a variable. At any rate, I wrote my registry with your suggestions and it worked flawlessly. The one issue that persists is the fact that no text shows up in my notepad application when I double click on a `.myext` file. When I select "Open with" and choose Notepad, the text shows up. I'm starting to think this is an issue with my code... – Rob Feb 19 '12 at 20:17
  • *> The one issue that persists is the fact that no text shows up in my notepad application when I double click on a .myext file. When I select "Open with" and choose Notepad, the text shows up. I'm starting to think this is an issue with my code.* @Mike, you said it works when you do it manually, so that should not be it. The problem remains that the (correct) file is not being opened. Try using `"%L"` instead of `%1`. – Synetech Feb 19 '12 at 22:42
  • @Synetech apologies, there *is* a `%L` syntax but it seems unnecessary (Doesn't Windows already pass in long file names with `%1`? Short 8.3 names are already outdated. You just have to ensure you have surrounding quotes.). @Mike Have you tried associating `.myext` to Windows Notepad? Does the file then open correctly? If yes, then the problem is in your Java code. May I ask, however, what exactly does your `mjar.jar` do with the file that is passed in to it using the `"%1"`? – ADTC Feb 20 '12 at 10:06
  • @ADTC I have tried associating `.myext` with Windows Notepad and it displays the text properly. I'm sorry but I'm not too sure I understand your last question. Is that where `@echo %1 @pause` comes into play (do I need to place this anywhere in my batch file or was it just used for "testing")? To "echo" the text to the application so it will display (if yes, are Windows applications designed to do this automatically?)? – Rob Feb 20 '12 at 13:22
  • @Mike my batch file was for testing, but that does not mean you can ignore the importance of `%1`. In fact, I suggested you to modify your batch file to `start /min "C:\...\javaw.exe -jar" "C:\...\mjar.jar" "%1"` (and create a new `run.exe` from it). Once you do this, when you double-click `file.myext`, the path of this file will be passed to `run.exe` **through the `%1` in registry**. This in turn will be passed to `mjar.jar` **through the `%1` in batch file**. Btw, your Java main method must get `args[0]` and use it (for whatever purpose). Not sure whether your Java code does that or not. – ADTC Feb 20 '12 at 15:41
  • In other words, if you don't have a `%1` in your batch file, the associated file's file name that is being passed by Windows to your `run.exe` is simply thrown away, and not passed to your `mjar.jar`. Your Java code will not receive the file name without a `%1` in the batch file (in the correct place, of course). I hope it's clear now. [In a DOS batch file, the term %1 will be replaced by the first argument that is passed in when you execute it. Basically, it's a variable representing the command-line argument.] – ADTC Feb 20 '12 at 15:51
  • @ADTC So, I've updated all my code to match your suggestions and I haven't had any luck. I have a main method in my code that accepts `(String[] args)` so that shouldn't be an issue. I'm really not too sure what else to do... – Rob Feb 20 '12 at 20:30
  • @Mike it's good you have a main method that accepts `(String[] args)` but does the main method use that String array? For starters, you may want to do a `System.out.println("Testing argument: " + args[0]);` to test whether your Java program is getting the file name. If that works (you should get a line in command window that says `Testing argument: C:\whatever\path\file.myext`), then any problem that remains is purely in your code. [If the command window opens and closes quickly, add a `pause` line at the end of your **batch file**.] – ADTC Feb 21 '12 at 04:17
  • @ADTC he has already confirmed that the batch file works from the menu item and that the compiled batch file works when run manually, so it’s not the JAR file or the batch file. Somehow the compiled batch file is having difficulty running from the context-menu. `o.O` – Synetech Feb 22 '12 at 01:57
  • @ADTC I did receive a message in the console: `Testing argument: file.myext`, however the document still remains blank. – Rob Feb 22 '12 at 21:26
  • @Mike What do you get after `Testing argument:` when you **double-click** an associated file (_not_ when you run the command from a command window). Also, please ensure you have enclosed `%1` in quotes (as per my previous comment just now.) – ADTC Feb 24 '12 at 09:11
  • @Mike I have updated the answer as requested. Let me know how it is. Feel free to do any touch-ups as necessary or consult me if you're not sure but want to suggest any changes. – ADTC Feb 25 '12 at 23:10
  • @ADTC This looks great! I appreciate all of your help. – Rob Feb 25 '12 at 23:22