javaeclipse

How to run a (dot)java file


I've been looking around for a tutorial on how to compile/run a java file. I know you can run it and compile it with eclipse, and you can type "javac" in terminal (mac), but i would like to send a small program i wrote to a friend, and he can just click on it to run it. Maybe a class file, I don't know. I am running mac btw.


Solution

  • First of all create an executable JAR File and then-

    You can create-

    1. a batch file(.bat) for windows like this,

      @echo off
      start javaw -jar XYZ.jar
      

      save this file to ABC.bat, now this file is executable, just double click on it and run your program.

    2. an executable file(.exe) for windows, like this,

      This is a C program-

      ‪#‎include‬ <string.h> 
      #include <stdlib.h>  
      
      void main (int argc, char **args) {
          char *jarfile = "XYZ.jar"; // jar file name...
          int n = strlen(jarfile);
          char *cmd = (char *)malloc(n+50);
          strcpy(cmd, "startw java -jar ");
          strcat(cmd, jarfile);
          system(cmd);
      }
      

      compile this file with any c compiler, now run the executable file(.exe).