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

前端 未结 11 2605
终归单人心
终归单人心 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:41

    Create a plain text file (using notepad) and type the exact line you would use to run your Java file with the command prompt. Then change the extension to .bat and you're done.

    Double clicking on this file would run your java program.

    I reccommend one file for javac, one for java so you can troubleshoot if anything is wrong.

    • Make sure java is in your path, too.. IE typing java in a Dos windows when in your java workspace should bring up the java version message. If java is not in your path the batch file won't work unless you use absolut path to the java binary.
    0 讨论(0)
  • 2021-02-15 14:41

    hey I think that you just copy your compiled class files and copy the jre folder and make the following as the content of the batch file and save all together any windows machine and just double click

    @echo
    setpath d:\jre
    d:
    cd myprogfolder
    java myprogram
    
    0 讨论(0)
  • 2021-02-15 14:45

    I just made a simple batch script that takes a file name as an argument compiles and runs the java file with one command. Here is the code:

    @echo off
    set arg1=%1
    shift
    javac -cp . %arg1%.java
    java %arg1%
    pause
    

    I just saved that as run-java.bat and put it in the System32 directory so I can use the script from wherever I wish.

    To use the command I would do:

    run-java filename
    

    and it will compile and run the file. Just make sure to leave out the .java extension when you type the filename (you could make it work even when you type the file name but I am new to batch and don't know how to do that yet).

    0 讨论(0)
  • 2021-02-15 14:46

    Am i understanding your question only? You need .bat file to compile and execute java class files?

    if its a .bat file. you can just double click.

    and in your .bat file, you just need to javac Main.java ((make sure your bat has the path to ur Main.java) java Main

    If you want to echo compilation warnings/statements, that would need something else. But since, you want that to be automated, maybe you eventually don't need that.

    0 讨论(0)
  • 2021-02-15 14:46

    I do not have a JDK installed on my machine to verify this, but here's something to get you started with

    %CLASSPATH%=C:\Program Files\Java\jdk1.6\bin rem or whatever your path is
    START %CLASSPATH%\javac Main.java
    START %CLASSPATH%\java Main
    
    0 讨论(0)
提交回复
热议问题