[英]javac and java commands through Windows batch files
I normally compile things through the command line using: 我通常使用以下命令通过命令行进行编译:
javac -classpath . Test.java
Similarly, I run them through: 同样,我通过以下方式运行它们:
java -classpath . Test
I'm now attempting to save myself the trouble of typing these out every time through batch files. 我现在正试图省去每次通过批处理文件键入这些代码的麻烦。 I have attempted to do so through another question from here:
我试图通过这里的另一个问题来做到这一点:
Creating a batch file, for simple javac and java command execution 创建批处理文件,以执行简单的Javac和Java命令
I've also tried my own way: 我也尝试了自己的方式:
cmd.exe
@echo off
javac -classpath . Test.java
Still no luck, however. 但是,仍然没有运气。 I have checked that my PATH environment variable is correctly pointing to the latest version of jdk and as I've said, I can compile just fine directly through command line.
我检查了我的PATH环境变量是否正确指向了最新版本的jdk,正如我所说,我可以直接通过命令行进行编译。 Upon running the batch file, I just get the command prompt with no error;
运行批处理文件后,我得到的命令提示符没有错误; as if there was nothing under cmd.exe.
好像cmd.exe下没有任何内容。 Could anyone lend a helping hand and slap some sense into me?
谁能伸出援手,向我扑过来?
When you write cmd.exe
, that will start a new command prompt. 当您编写
cmd.exe
时,将启动一个新的命令提示符。 You don't want that. 你不要那样
When you write @echo off
, that means nothing will be printed on the screen after that point. 当您写
@echo off
,这意味着在此之后屏幕上将不会打印任何内容 。 That's what it means. 就是这个意思。 That's what it does.
这就是它的作用。 That is why it looks like nothing is happening.
这就是为什么看起来什么都没有发生的原因。
Something would be printed to the screen if you had a compilation error, but probably you don't. 如果您遇到编译错误,某些内容将被打印到屏幕上,但可能没有。
If you want the command prompt window to stay around instead of disappearing, I believe there is an option in Windows to configure that, at least there was when I last used Windows, back in the mists of time. 如果您希望命令提示符窗口停留在周围而不是消失,我相信Windows中有一个选项可以配置,至少在我上次使用Windows时是这样。
this worked for me. 这对我有用。 I think it does what you were looking to do.
我认为它确实可以满足您的需求。
This is the code I suggest for the .bat file: 这是我建议的.bat文件代码:
cd C:\Users\John\JavaApps\folderThatContains.java //points terminal to folder
javac Main.java //This compiles .java in said folder
cmd /K "java Main" //cmd /K prevents terminal from quitting after "java Main"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.