Execute a Java program from our Java program

You’re trying to execute “C:/”. You’ll want to execute something like:

"javaw.exe d:\\somejavaprogram\\program.jar"

Notice the path separators.

I’m assuming this is for an ad-hoc project, rather than something large. However, for best practice running external programs from code:

  • Don’t hardcode the executable location, unless you’re certain it will never change
  • Look up directories like %windir% using System.getenv
  • Don’t assume programs like javaw.exe are in the search path: check them first, or allow the user to specify a location
  • Make sure you’re taking spaces into account: "cmd /c start " + myProg will not work if myProg is "my program.jar".

Leave a Comment