1

I want to make a "launcher" for my jar program, basically my exe file doesn't do anything by its own, its simply for launching my jar program.

My jar program can open a file, and as far as I know (pay atention to this because maybe here is the problem) the OS opens a file by passing a command line argument to the program that contains the path to the file, right?

If I execute my program using the play button in Visual Studio it works fine, I put the arguments in Properties->Debug->Command line arguments, it launches my jar and opens my file

If I execute my program from CMD, passing the argument through cmd as well, it works fine and once again it opens mi file

But if I do right click on the file that I want to open, then "open with"... I get the System.ComponentModel.Win32Exception

I thought, maybe the program needs to be installed, so I created a innoSetup installer, but if I do right click->open with... I get Win32Exception

What am I doing wrong?


My code :

 class Program
        {
            static void Main(string[] args)
            {
                var programa = new System.Diagnostics.Process();
                programa.StartInfo.UseShellExecute = true;
                programa.StartInfo.FileName = "Personas.jar";
                String archivo ;
                try
                {
                    archivo = args[0];
                }catch(System.IndexOutOfRangeException e)
                {
                    archivo = null;
                }
                if(archivo != null)
                {
                    programa.StartInfo.Arguments = archivo;
                }
                try
                {
                    programa.Start();
                }catch(Exception err)
                {

                }
            }
        }

I won't put the Java code because the problem is clearly in C#

The exception

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • It's simply because jar are plain archives rather than PE executables. You will have to launch it with java -jar nameOfJar.jar, though I don't read Spain neither understand C#, I believe it's the cause. – glee8e Jan 08 '17 at 15:54
  • What do you right click on? And what do you select in the dialog box that follows after "open with"? Hint: check the working directory – Thomas Weller Jan 08 '17 at 19:23
  • @glee8e: as long as JAR files are associated with Javaw.exe it will work – Thomas Weller Jan 08 '17 at 19:24
  • Also try: run `cmd`, go to `c:\ ` and then run the program with its full path. Will probably not work either – Thomas Weller Jan 08 '17 at 19:25

1 Answers1

0

When you are launching your application using Visual Studio the current directory is set to that of the executable being launched. I presume your personas.jar file is in the same directory as your executable so when you open your jar file using ShellExecute = true it finds the file looks up the association and runs successfully.

When using CMD you're probably launching it from within the executable directory so the current directory is set as above and everything works as planned once again.

But when you open a file using Open With... the current directory is set to the directory containing the file that was right clicked on. That means when you try to open personas.jar using ShellExecute it looks in the clicked on file's directory and can't find it. So failure.

You need to get the path to the executing assembly and use that to either set the current directory or fully qualify the path to personas.jar.

Also, using the Arguments property of the StartInfo object while opening a file by association is not a documented use. So while it may work now it may stop working at any point.

Stephen Martin
  • 9,495
  • 3
  • 26
  • 36
  • what you said makes sense, but I also tried to copy my file in the same directory of my application, so the current directory is the same for both of the files, my jar and the file that I want to open, it didn't work either. Anyways I'm going to try the full path to my jar file and I'll let you know if it works. Thanks – Giovanni El Zelah Jan 09 '17 at 15:46