-1

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:

.\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.

I don't know how to open the .bat file from the Java code.

The image of project folder "After Installation"

The code of executing .bat file is written under the one of the project packages.

Please help me. Thanks.

Mustafa Mohammadi
  • 1,433
  • 1
  • 12
  • 25
  • I am not very clear about your question, but seems you are looking for `ProcessBuilder` to execute a `.bat` file? – Wilson Jul 02 '16 at 08:10
  • Yes, but the addressing is an important problem. I can not address absolutely. – Mustafa Mohammadi Jul 02 '16 at 08:24
  • seems you are looking for a way to get the path of a running jar. See http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file whether can help – Wilson Jul 02 '16 at 08:27
  • Thanks, here is one more question. As you see in the picture, I converted .jar file to .exe. Does it have any problem? Thanks – Mustafa Mohammadi Jul 02 '16 at 08:34

1 Answers1

1

You must use the Runtime.exec() method:

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.

Viruzzo
  • 3,025
  • 13
  • 13
  • The problem is here that, I can't use an absolute path for this. This is a product. I want to address how, I don't face to problem after creating setup. – Mustafa Mohammadi Jul 02 '16 at 08:22
  • 1
    @MustafaMohammadi using an absolute path is not your problem then, it's how to find the path out: see Wilson's comment above for a way. – Viruzzo Jul 11 '16 at 14:25