I'm developing a project in Java. When I export the project in .jar, I need to start mysqld to run my database. I start mysqld using a .bat file which this file is needed to be opened every time user opens the main file to access to database. The batch file content is:
我正在用Java开发一个项目。当我在.jar中导出项目时,我需要启动mysqld来运行我的数据库。我使用.bat文件启动mysqld,每次用户打开主文件访问数据库时都需要打开该文件。批处理文件的内容是:
.\data\bin\mysqld.exe --no-defaults --console --port=3306 --collation-server=utf8_persian_ci --character-set-server=utf8 --socket=mysql.sock --basedir=.\data --datadir=.\data\data --pid-file=".\data\data\MysqldResource.pid pause
。\ data \ bin \ mysqld.exe --no-defaults --console --port = 3306 --collation-server = utf8_persian_ci --character-set-server = utf8 --socket = mysql.sock --basedir =。 \ data --datadir =。\ data \ data --pid-file =“。\ data \ data \ MysqldResource.pid pause
The data folder contains database files and is placed in the same folder with .jar and .bat files.
数据文件夹包含数据库文件,并与.jar和.bat文件放在同一文件夹中。
I don't know how to open the .bat file from the Java code.
我不知道如何从Java代码中打开.bat文件。
The code of executing .bat file is written under the one of the project packages.
执行.bat文件的代码是在其中一个项目包下编写的。
Please help me. Thanks.
请帮我。谢谢。
1 个解决方案
#1
1
You must use the Runtime.exec() method:
您必须使用Runtime.exec()方法:
Process batch = Runtime.getRuntime().exec("cmd /c D:\\Projects\\Terminal\\project Deployment\\run_server.bat");
Process batch = Runtime.getRuntime()。exec(“cmd / c D:\\ Projects \\ Terminal \\ project Deployment \\ run_server.bat”);
Check the path for correctness; it's advisable to use an absolute path, otherwise keep in mind that the starting path is going possibly to be your user home, depending on how Java is run.
检查路径是否正确;建议使用绝对路径,否则请记住,起始路径可能是您的用户主页,具体取决于Java的运行方式。
#1
1
You must use the Runtime.exec() method:
您必须使用Runtime.exec()方法:
Process batch = Runtime.getRuntime().exec("cmd /c D:\\Projects\\Terminal\\project Deployment\\run_server.bat");
Process batch = Runtime.getRuntime()。exec(“cmd / c D:\\ Projects \\ Terminal \\ project Deployment \\ run_server.bat”);
Check the path for correctness; it's advisable to use an absolute path, otherwise keep in mind that the starting path is going possibly to be your user home, depending on how Java is run.
检查路径是否正确;建议使用绝对路径,否则请记住,起始路径可能是您的用户主页,具体取决于Java的运行方式。