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

前端 未结 11 2601
终归单人心
终归单人心 2021-02-15 14:18

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 whi

11条回答
  •  星月不相逢
    2021-02-15 14:40

    The only other thing I would add is to make it a tad more flexible. Most times I'll have a trivial java file I want to run like - Main.java, Simple.java, Example.java, or Playground.java (you get the idea).

    I use the following to strike off a javac and corresponding java.

    @echo off
    javac %~n1.java
    java %~n1
    

    The %~n1 gets the filename (sans extension) of the first argument passed to the batch file. So this way I can run it using tab completion and not have to worry about it working with either the .class or .java extension.

    So both of the following will have the same result:

    run.bat Main.class
    and
    run.bat Main.java
    

    Doesn't 100% answer the original posters question, but I think it is a good next step/evolution for simple javac/java programs.

提交回复
热议问题