0

I want to run a jar file in both unix and windows without have to call it directly with java like:

java -jar myjar.jar parameters

i want :

myjar.jar parameters

I've been reading allready -

Running a JAR file without directly calling `java`

Which seems like a very nice hack for unix .

Howerver , this wont work in windows.

I'm looking for a uniform solution that will work both on unix and windows , but I'm not sure there is such.

The solution has to be only once , and it has to include changes related to the jar only ,and not the operation systems - because this is a file to I'm suppling to a client.

Community
  • 1
  • 1
RanZilber
  • 1,840
  • 4
  • 31
  • 42
  • Downlvoted with no explanation. Stackoverflow passive agressiveness strikes again. I upvoted it because it's a good question and the first to appear in Google search results. – Sridhar Sarnobat Dec 12 '17 at 23:28

3 Answers3

1

What you are asking can't be done: Windows will load executable files only in the PE/COFF format used in .exe in .dll files.

What you can do instead is supply the users a "wrapper" program that starts the actual Java program. You could create the wrapper in C, which has several benefits: you can set an icon on the executable and associate the program with file types in the Windows Explorer. Batch files are a popular alternative; they are easier to create.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • "JAR files that can be run with the java -jar option can have their execute permissions set so they can be run without using java -jar. See the JAR File Overview at http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jarGuide.html" (in Http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html#CBBIJCHG) So what's Oracle saying? – Jr. Jun 07 '17 at 18:49
  • @Jr. The question was about "running jars *without* Java" i.e. directly at the o.s. level – Joni Jun 07 '17 at 19:12
  • I highly don't think so. It is evident on his example and 'hack in linux'. – Jr. Jun 07 '17 at 20:05
  • The "hack" adds kernel level support for jar files, so they can be executed alongside ELF binaries. (Internally it launches a JVM, but that's transparent to the user). This is not possible on Windows, since no similar kernel module exists. – Joni Jun 08 '17 at 19:04
0

You can provide a script that starts your application for both Unix (.sh) and Windows (.bat). This seems to be the preferred approach for many companies. An example would be JBoss Server where a run.bat is provided for Windows and a run.sh is for Unix. These scripts set the appropriate environment variables, classpath, etc and then call java.

Eduardo Sanchez-Ros
  • 1,777
  • 2
  • 18
  • 30
0

You can write your own bash and batch/powershell scripts. As for windows, you can try Launch4J. It should be easier than writing elaborate scripts from scratch.

Be aware that you can only provide wrappers to make the execution simpler (one click). Java has to be run anyway. Either explicitly, by the user, or as part of a script. You can't do without it.

Community
  • 1
  • 1
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131