How to compile and run Java with single command line on Windows

Viewed 3211

Everytime I want to build and run my program I do:

javac myProgram.java
java myProgram

I want to do something like this:

buildrun = javac (some_argument).java && java (some_argument)

so after I can just

buildrun myProgram

How to achieve this on Windows?

4 Answers

To compile and run Java with single command line in cmd use: java myProgram.java

You can use Makefile this way:

  1. Create a file named Makefile within the same folder where your java files reside.
  2. Add these contents to Makefile
run: compile
    java $(class)

compile: 
    javac $(class).java
  1. From terminal, run this command: make run class=myProgram

This way, it will compile first then run your java class in a single command

Related