Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a batch file, for simple javac and java command execution

It is a really simple thing but I cannot get my head around it. I have looked at plenty of StackOverFlow post and as well as on internet.

My goal is to create a .bat which will open cmd and execute my Main.java into command prompt. Easy isn't it, but I have got confused about how?

Below I am writing steps which my batch file should perform

  1. open cmd
  2. javac Main.java
  3. java Main

My file will reside next to all my .java so I am assuming I don't need to give explicit path.

My understanding so far by research:

  1. open text editor
  2. write a code to open cmd(Which I am not sure how)
  3. echo javac Main.java (Which is still fuzzy concept for me)
  4. echo java Main

Save as: name.bat

Any help will be appreciated. Thank you.

like image 639
TeaCupApp Avatar asked May 27 '11 03:05

TeaCupApp


2 Answers

  1. open notepad

  2. write

    @echo off
    
    javac Main.java
    
    java Main
    

3.saveAs blahblah.bat

make sure that Main.java resides with your batch file and java path is set in env. variable

4 . double click on batch file, no need to open cmd explicitly tt will open itself on .bat execution

like image 124
Nirmal- thInk beYond Avatar answered Sep 21 '22 00:09

Nirmal- thInk beYond


you want these four lines of code in your Run.bat:

@echo off          //this makes it so you have an empty cmd window on startup
javac Main.java    //this compiles the .java into a .class
java Main          // this runs the .class file
pause              //this prevents the window from instantly closing after program end
like image 24
David Avatar answered Sep 18 '22 00:09

David