简体   繁体   中英

Problem with running jar file in windows10

I have a java project, which I converted into jar file. And later, my C# project is using that jar file with this code

proc = new Process();
            proc.StartInfo.WorkingDirectory = jarDir;
            Console.WriteLine("\nDirectory with jar file is\n" + jarDir);
            proc.StartInfo.FileName = jarName;
            Console.WriteLine("\npassed arguments are" + ifs_id.ToString() + " " + max_record.ToString() + " " + max_time.ToString() +
                " " + outputName + " " + outputDir);
            proc.StartInfo.Arguments = " " + ifs_id.ToString() + " " + max_record.ToString() + " " + max_time.ToString() +
                " " + outputName + " " + outputDir;
            proc.EnableRaisingEvents = true;
            proc.Exited += new EventHandler(OnProcessExit);
            proc.Start();

And all was working prefectly. Problem begins when I transfered my project to other computer, also working under windows10. It dosent work. Process starts, and imidiately finishies. Whats more, I cant run jar file by double clicking on it, and I think that this might be the case. When developing code, everything works perfectly. But when i converted project to.jar file, it stops working.

Its not a problem with directories, I double checked them.

I have no ideas what my be causing this situation. I will realy appretiate any advices.

Check the following:

  1. Does the.jar file which your program calls reside on the computer that you moved the program to? You can't use that Process class if you need to access the.jar file from a remote server.

  2. When you say it "stops working" does it throw an error at all?

  3. Did you check if java is installed on the computer that you moved the program to (run java -version in command line)?

Your code: proc.StartInfo.FileName = jarName; tells Windows to run the.jar file with whatever it has associated as a handler for jarfiles. It is the same as double clicking.

So the fact that both methods don't run means the Java runtime on Windows isn't a registered handler to the jarfile. Depending on the JRE flavours you have installed, most of the Open JRE installers don't do it. AdoptOpenJDK has started including the jarfile handling option into the installer i think some time this year(?).


Option 1

Fix the jar file association. This should make it work on your other computer, but you would need to do it on every other computer that are having the same issue.

On the bright side, this will also fix double-clicking on the jar, something that Option 2 below doesn't do.

You have a few options to do this:

  • Use a JRE/JDK installer that does it for you (eg AdoptOpenJDK 's installer).
  • Do it manually. This particular answer (not the one marked as accepted ) should work.
  • If you're not against running a third-party tool made by a random stranger on the internet, jarfix would automate the manual way above.

Option 2

Make your program trigger Java even if it's not currently associated with jarfiles. This will likely make it run on more computers, but won't fix your jar double-click issue.

This requires Java to already be installed and is in your OS's PATH. To test this, fire up a command prompt, and run java -version . If you get a response, keep reading. If you don't, you need to look up how to include Java in your PATH.

Now, in your StartInfo , you want to call java and not your jarfile directly:

proc.StartInfo.FileName = "java";

You then specify your jar as part of the argument:

proc.StartInfo.Arguments = " -jar " + jarName + " " + ...the rest of your args.

You can replace "java" with "javaw" if you know you'll only ever run it on Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM