如何在不同目录中运行bat文件 [英] How to run bat file in different directory
问题描述
我有一个 bat 文件,它运行一个名为 Systemm.java
的 java 程序,位于一个名为 tp3
我的文件组织如下
<预><代码>.├── src├── tp3│ ├── Systemm.java│ ├── Communication.java│ └── exec_server.bat└── teste.bat所以,我的主要问题是,我使用 teste.bat 来运行我的类 Systemm.java.这个蝙蝠很好用.
我还想做的是在 java 中调用 exec_server.bat.
依次:
- teste.bat 运行 Systemm.java
- Systemm.java 有一个类 Communication.java 的实例
- 这个实例运行一个应该运行 exec_server.bat 的方法
通信executeServer()方法
public void executeServer() {尝试 {//小路Process p = Runtime.getRuntime().exec("cmd/c start \"\" tp3\exec_server.bat");p.waitFor();System.out.println(我完成了你的服务器运行");} catch (IOException | InterruptedException e) {e.printStackTrace();}}
但是,当我运行我的程序时,此方法在 cmd 上显示此消息
我几乎是探索 cmd 和 bat 文件的新手,所以我真的很困惑我做错了什么.
非常感谢您提前提供帮助!
P.S:我在 src 目录上调用 cmd,exec_server.bat
只回显消息你好!";在 cmd 上
请提供完整路径试试:
进程 p = null;尝试 {p = Runtime.getRuntime().exec("cmd/c start D:\\practice\\P1\\samp.bat ");} catch (IOException e) {//TODO 自动生成的 catch 块e.printStackTrace();}
I have a bat file which runs a java programm called Systemm.java
, located within a package called tp3
My file organization is as follows
.
├── src
├── tp3
│ ├── Systemm.java
│ ├── Communication.java
│ └── exec_server.bat
└── teste.bat
So, my main problem is, I'm using teste.bat to run my class Systemm.java. And this bat works fine.
What I am also trying to do is call exec_server.bat in java.
Sequentially:
- teste.bat runs Systemm.java
- Systemm.java has an instance of class Communication.java
- This instance runs a method that should run exec_server.bat
Communication executeServer() method
public void executeServer() {
try {
// Path
Process p = Runtime.getRuntime().exec("cmd /c start \"\" tp3\exec_server.bat");
p.waitFor();
System.out.println("I finished running your server");
} catch (IOException | InterruptedException e) {e.printStackTrace();}
}
However when I run my programm, this method displays this message on cmd
I am pretty much the newbie exploring cmd and bat files, so I am really confused as to what am I doing wrong.
Thank you so much for any help in advance!
P.S: I' calling cmd on the src directory, and exec_server.bat
only echos the message "Hi there!" on cmd
Please try with by giving full path:
Process p = null;
try {
p = Runtime.getRuntime().exec("cmd /c start D:\\practice\\P1\\samp.bat ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于如何在不同目录中运行bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!