7

Java program needs to be packaged to JAR file so it can be executed using java -jar command. So why don't I have to execute javac with java -jar javac command? How did Sun/Oracle make java program into executable binary file?

I know there are tools that can convert jar file to windows executable file. But I want my jars to be executable in Linux/OS X without the help of bash script.

---------- UPDATE

I found this link really helpful: https://github.com/maynooth/CS210/wiki/Convert-Java-Executable-to-Linux-Executable

Neo
  • 2,196
  • 5
  • 32
  • 58
  • 2
    I believe it was written in C (mostly). If you want, you can do some research into native executables for Jars – MadProgrammer Aug 10 '15 at 05:25
  • 2
    @MadProgrammer: no it's not. `javac.exe` is just a thin wrapper around calling the corresponding Java class. It starts up the VM (similar to the way `java.exe` does) and then calls the Java compiler –  Aug 10 '15 at 05:41
  • @a_horse_with_no_name Do you have a reference for that? – MadProgrammer Aug 10 '15 at 05:42
  • 1
    `javac` is itself written in `Java`. @MadProgrammer For reference - https://en.wikipedia.org/wiki/Javac – Ankit Sharma Aug 10 '15 at 05:42
  • @AnkitSharma Thank you – MadProgrammer Aug 10 '15 at 05:44
  • 3
    @MadProgrammer: You can easily run: `java -cp tools.jar com.sun.tools.javac.Main` and it will do exactly the same as `javac.exe` will. Plus I think you can find hints for that in the source code for java.exe (see `launcher/java.c` in the source directory) –  Aug 10 '15 at 05:45
  • @a_horse_with_no_name Thank you – Neo Aug 10 '15 at 06:07

1 Answers1

4

If javac was written in Java, why I can execute javac as if it is a none-java program?

The answer to your question has already been given by Jon Skeet for the question Why Java compiler as distributed as executable and not as JVM bytecode?

Quoting his answer here below

javac.exe (on my installation, JDK 1.8 on Windows x64) is about 15K in size. This isn't the full compiler. The compiler itself really is written in Java, and javac.exe is just a launcher, effectively. This is true of many of the tools that come with Java - it would be a pain to have to run something like:

java -cp path/to/javac.jar java.tools.Javac -cp path/to/your/libraries Foo.java

A simple way to understand the whole thing is imagining JRE(Java runtime environment) acting like an intermediate layer between your program and the OS. JRE accepts the bytecode and runs your java program.The javac (java compiler ) converts your java source code to platform neutral byte code(exceptions are there). I am not sure if java,javac,jre is written in java or not. But if they are,then they have to linked/loaded in a different way in different OS(platforms).

Now coming to how to run jar in windows and linux

Normally java code is converted to jar file. Then there will be two files(mostly along with jar file) to start the jar file , one for windows and one for linux

For example Apache tomcat has files (in same location)

startup.bat ==> to start program in windows.
startup.sh  ==> to start program in linux.

Alternately you could convert jar to exe for windows. For linux the link you specified is enough. The script is interpreted by command interpreter in linux and your jar file will be executed.

This link specifies different ways of executing a shell script in linux. http://www.thegeekstuff.com/2010/07/execute-shell-script/

This link has code to run jar as bat Run .jar from batch-file

To sum it up , your jar file can be platform independent but they way to start the jar file will differ for different platforms.

Community
  • 1
  • 1
Raj
  • 1,945
  • 20
  • 40