How to write a batch file to run java class/ jar file

Posted by
Without using any third party utility for making your class file/jar file executable, make an executable file for running class/jar files,write a batch file to run java class/ jar file
Just using some batch commands you can run your file. 

Make batch file for running class file :

Copy the following code into notepad and save it as “filename.bat”.

  1.  First line is for compiling the java file if you have class file and don’t want to compile, you can skip first line.
  2. Second line is for running your class file “>output.txt”  will save your output in output.txt file in the same location where your .bat file is saved.

Note: If you have your .bat file in the same location where your java/class file stored then don’t need to give the path.

1:  javac your_file_pathTest.java  
2:  java Test > output.txt

In case you just want to run your class file and don’t want to keep the .bat file in the same location where you have stored your class file then use following code:

1:  D:  
2:  cd JAVAPROGAbstract  
3:  java Test > output.txt  

Your executable file is ready to run your class file , just double click on .bat file and enjoy!!!! 

Make batch file for running jar file :

Use following code to execute your jar file.If you are using multiple jar files in your java application then keep your .jar file (The one you want to execute) , lib folder (the one which have all external jar files) and .bat file at one location.
1:  java -jar "pathtTesting.jar"   
Your executable file is ready to run your jar file , just double click on .bat file and enjoy!!!!

Leave a Reply