java 批处理文件,如何执行从Java批处理文件?

在Java程序中,使用Runtime.exec()运行相对路径的批处理文件时,路径是相对于当前用户目录的。可以尝试使用"cmd.exe"结合"/c"参数和相对路径,如"./com/projct/util/server.bat",或者获取用户目录并拼接绝对路径。确保在正确目录下启动Java程序,或者动态计算绝对路径来实现跨环境部署。
摘要由CSDN通过智能技术生成

I want to execute a batch file from a java program.

I am using the following command.

Runtime.getRuntime().exec("server.bat");

But the problem is I want to give a reative path instead of absolute path so that I can deploy that java project on any comp.

The dir structure of the project is like as follows:

com

|

project

|

------ parser

| |_____ Main.java

|

-------util

|_____ Server.bat

I want to run the "Server.bat" file in the "util" dir from the "Main.java" file in the "parser" dir.

解决方案

When Java is running and you use Runtime.exec() with a relative path, relative means relative to the current user direcory, where the JVM was invoked.

This may work

Runtime.getRuntime().exec("cmd.exe", "/c", "./com/projct/util/server.bat");

if you start java from com's parent directory.

Or you must calculate an absolut path:

Runtime.getRuntime().exec("cmd.exe", "/c",

System.getProperty("user.dir")+"/com/projct/util/server.bat");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值