Opening ".bat" file in java code - java

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 code of executing .bat file is written under the one of the project packages.
Please help me.
Thanks.

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.

Related

Batch file not executing the required commands in windows 8.1

I have created a batch file to set the path of a jar file whenever i need to use that jar file.But even after executing the file,the system is unable to recognize the jar file and when i compile my java program which uses that jar file,it gives compile time error(i.e the path is not set).
And when i simply use the classpath command in command prompt which i wrote inside the batch file,it works.
But i want to make a batch file so that whenever i need to set classpath,i can use that batch file.
Help will be appreciated.
Following is the batch file.
set classpath=jsoup.jar;.;%classpath%
Solution found by OP:
Thanks,I solved the problem.I just made a .cmd file containing the command.Now whenever i will need to set the path i will run that cmd file in command prompt.

How to export only the Client to a jar from a RMI - Eclipse

I have created for a first time a programm in Java SWING including RMI Connection with PORT (Server - Client) in Eclipse.
Now I want to export only the Client (to a jar File if its possible ) and test it (via install it) to another computer so I could see if it works and how it works.
The Structure of my project in Eclipse is like :
From there I want only the Client to export and install it to another PC and start the Programm with the LoginForm.java. The .jar export files that have tryed and created they dont do nothing at all. Also dont know if I have to do something specific with the Build Path or the folders (like Image folder) that I have extra create.
JavaMunich:
Hava try it and nowthing happens when I double cliked it
You can't just double click a .jar file, you can execute the program by using the following command.
java -jar YOURJAR.jar
You could make a .bat file with the command in it to make it easier for future executions.
Open notepad.
write :- java -jar YOURJAR.jar.
Save it with the extension .bat.
copy it to the directory which has the .jar file.
double click it to run your jar file.

2: No such directory error when running jar from bat file using scheduler

I am trying to schedule up java application using windows scheduler.
I have created bat file where is written: java -jar C:\....(full path)\myJar.jar
Java is using data folder, located right next to jar file, during execution. Everyone has access to the data folder so permission is not the issue as far as I understand.
The way I access to the folder in java is by setting path: "data\\test.csv"
Please note that bat file is located next to jar file and the data folder.
Interesting is that if I run the bat file manually then everything works fine, when I run it from scheduler, error occurs.
I have solved it by adding cd line to bat file. So before execution of jar, directory is changed.
Bat file would look like this:
cd C:(full path to directory)
java -jar myJar.jar
pause

Can I make a jar file runnable from any directory?

I am curious if I can take a .jar file and somehow add it to my classpath so that I can run it from any directory. For example, let's say I have a .jar located at /home/setup/someJar.jar. Is there a way I can run this from another directory (preferably any) so that I do not have to navigate back to /home/setup/ whenever I want to run it?
I tried adding the path to my .bash_profile file by adding :/home/setup to the PATH= line, but didn't work.
I think you probably want an 'alias'.
http://www.linfo.org/alias.html
alias myJarShortcut="java -jar /direct/path/to/nameOfYourJar.jar"
Add this to your .bash_profile and it will be available every time you boot up.
It's possible but not so easy. you have 3 possiblities
Make an alias or symbolic link (create symbolic link)
Start it as deamon
start it as service (create service)
after ready the information above
you can start it like this
service [yourservice] start|stop|restart
You could create a bash script that execute your .jar file in the directory of your choice, with of course the right path to your .jar file.

Java file access

At the moment, in my java program, I am accessing a file that is in the project folder. When I'm loading the file its path is "./src/package/package/file.txt". When I build the program into an executable it dosen't work.
I would prefer the files to be outside of the .jar, but in the same folder, how would I got about this?
Samishal
You can use a relative path if they are in the same folder, such as ./file.txt. That should carry over even with a compiled JAR.
Otherwise, if you're going to be using the same machine and are confident of the placement of the files, you could use an absolute path, however I don't recommend it.
You can use Class.getResourceAsStream to get the InputStream. It works for jar package too. It is not possible to access the file in jar file using File API directly.
InputStream ins = Class.getResourceAsStream("/gigadot/exp/resource.properties");
More details at http://blog.gigadot.net/2010/10/loading-classpath-resources.html
Because the relative path is relative to where your command prompt is when you execute the program, not from where the program lives.
You somehow need to tell the program where to find resources. Most people use a .sh/.bat script to figure out where the script itself lives, and either pass -D flags or set the classpath based on that location.
as a note, I $0 gives you the script as it was run on the command line in linux (it could be relative or absolute), and you can use dirname from there to find it's directory, and alter the java command line.
in windows %~dp0 gives you the directory of the batch script which was run, and you can use that to form your java command line.

Categories