I have a java program (jar) that uses ProcessBuilder to execute a dynamic batch file.
Process process = (new ProcessBuilder(commands)).start();
I have a simple batch file like this, Z --> is a network drive. let say my batch file location is in my_batch.bat.
copy Z:\Download\*.csv D:\Download\
if I execute this batch file from the command prompt, it run successfully. But if I execute using the java program, the program does not work (nothing happen). But if I change the batch file like below, it works. E-> is a regular drive (not a network drive).
copy E:\Download\*.csv D:\Download\
I don't know why this can happen, please help.
My Operating System is Windows.
I solved it by putting this line in my batch file.
net use Z: \\Computer\shared /user:DOMAIN\username password
and after the process, use this at the end of my batch file.
net use Z: /d
thanks to the man that help me in the comment section.
Related
Summary:- I need lots of dynamic data for my performance testing and it's not possible to generate those test data from Jmeter itself. Hence, I wrote a Java code which will generate these dynamic test data and will put those data into the excel file. This excel file can be consumed by JMeter script for the performance testing. Every iteration in JMeter needs a new set of test data and that's why I have created a bat file which will trigger the Maven execution(it's just mvn clean test) and will generate the fresh set of test data before each of iteration. Everything is working fine till this point. I just need to run the bat file from JMeter to trigger the test data creation before each iteration and that's the problem which I am facing
Problem:- As mentioned in the link How to run batch file(.bat) from Jmeter and as suggested by user #Dmitry T, I have added the OS sampler with the given parameters(See the screenshot below) but it is not starting the Maven execution. It is hitting the bat file(I put some msg command to check) but somehow it is not starting the execution. I tried the other solution given by the same user about using the Beanshell Sampler and running the command
Runtime.getRuntime().exec("C:/Windows/System32/cmd.exe /c D:/XXXX/XXX/XXXX/GenerateTestData.bat");
This is also not working. Am I missing something here? Please let me know if there is any solution for this? Appreciate any help on this?
The batch file is most likely not designed to work properly with current directory on execution being different to the directory containing the batch file. The current directory can be any directory. Very common are the directories %SystemRoot% (Windows directory) and %SystemRoot%\System32 or %SystemRoot%\SysWOW64 (Windows system directory) as current directory, whereby any directory can be the current directory on running a batch file.
A batch file referencing other files or directories relative to the batch file directory should set the current directory to the batch file directory or reference all directories and files with full batch file path.
The argument 0 of a batch file is always the batch file itself. The help output on running in command prompt window call /? explains how to reference an argument with a modifier. In this case %~dp0 should be used to get full path of the batch file.
So in the batch file can be used at top:
#echo off
cd /D "%~dp0"
The current directory is set with second command line to the directory containing the batch file as long as the batch file is stored on a drive with a drive letter.
There is another method to make the directory of the batch file the current directory which works even with batch file being stored on a network resource and the batch file is executed using its UNC path.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
rem Other commands accessing files and directories in batch file directory
rem using no path or a path relative to current working directory.
popd
endlocal
The help output on running in a command prompt window pushd /? describes why this code works even with a UNC path on command extensions enabled which is made sure by the second command line which defines together with first command line completely the execution environment for the batch file without depending on configurations outside of the batch file.
Another solution is referencing all files and directories in batch file directory with full path which means with using %~dp0, for example "%~dp0ExcelFile.xlsx".
Note: The path string referenced with %~dp0 always ends with a backslash which is the directory separator on Windows as explained by Microsoft documentation about Naming Files, Paths, and Namespaces. Therefore concatenation of %~dp0 with another string like file/folder name or wildcard pattern should be done always without using an additional backslash for a 100% correct full file/folder/pattern argument string.
In the Command input provide full path to the cmd.exe
Change the Working directory to where your batch file lives
Use just batch file name in the Command Parameters
Something like:
See How to Run External Commands and Programs Locally and Remotely from JMeter article for more details.
Alternatively you can use Maven Exec Plugin to run your custom command before running the JMeter test
My Java application runs on a Windows service wrapper and the wrapper writes logs into a DEBUG file. The application has a service that backup and delete this DEBUG file at the end of the day. The service uses a script batch file to do this process.
My problem is, when the application try run this batch file and delete the DEBUG file following error occurs,
The process cannot access the file because it is being used by another process.
But if I run the batch file or run the relevant script manually everything works well. Is there a way to solve this problem without stopping the wrapper? (Tried to truncate the file but got the same error)
Script I use : DEL /A /F /Q "C:\MY_APP\logs\DEBUG.DMP"
Thanks!
php and batch files.
So i'm executing batch files through php
I have no issues launching the batch.
$str = exec('C:\WINDOWS\system32\cmd.exe /c START C:\MInecraft\_restart.bat');
However the issue im having is getting the batch file to work currectly. When the batch is run, it executes this code
taskkill /IM java.exe /F
java -Xmx1024M -jar craftbukkit.jar -o true
batch file successfully runs the taskkill command when launched with php, however it will not run the next line. When ran manually it launches fine, (bear in mind that the cmd.exe does not exit it stays open with this code)
Any ideas on how i can get this to launch from php?
Maybe the reason is the same as given at Error Executing Batch file of BlazeDS, the java executable is not found by Windows on running the batch file from within PHP script.
The current working directory or the directories listed in environment variable PATH can be different when the batch file is executed from within the PHP script in comparison to running the batch file manually.
Do you have ever tried to specify java.exe with full path in double quotes in the batch file?
You could also add at top of the batch file the commands
dir /w
PATH
to see which directory is the current directory and which directories are listed in environment variable PATH on execution of the batch file from within PHP script.
Forgot to post back here, I specified java through windows path command rather than the batch. The problem is because the java process takes the cmd and turns it into a console, it never finishes the batch file. So, the php never returns and continues the code unless the batch is force ably closed on the server.
pclose(popen("start /B C:\MInecraft\_restart.bat &", "r")); die();
I ended up using this command, which i believe makes it launch the batch and not wait for a reply. Just carry on with the php.
Thanks for your kind reply. I did initially try launching java in multiple ways.
I have Java Code that launch process of WinSCP tool and connects to a Unix machine and then call a xxxx.exe located on the Unix machine.
The problem is that xxxx.exe accepts a parameter of a type File. So I need to upload this to the remote machine and then passed to the xxxx.exe.... that is failing
and I'm trying to avoid the temporary folders as possible.
small Code
Process p = Runtime.getRuntime().exec("rTool\\WinSCP.com /script=folder\\code.txt < C:\\FILESTOUPLOADS\\upload1.txt" );
The login information goes in code.txt as supported by WinSCP.com
file redirection (i.e. the "<" symbol) is handled my the command processor, which Runtime.exec() does not use. As mentioned in comments already, first use the String[] version of exec so that you don't have issues with command parsing. second, you need to invoke the command processor to handle the file redirection (e.g. using "cmd.exe /k"), or handle it yourself in java.
Why not use ProcessBuilder to change working directory and set path of the file from that directory
public ProcessBuilder directory(File directory)Sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.
Parameters:
directory - The new working directory
Returns:
This process builder
I am creating a desktop application. I know how to add program to system tray, that consists of a continuous system process, I need instructions on how to add java code to system configuration startup menu. Like antivirus program which automatically executes on starting the system. would be of great help with example code
Write a batch file(.bat) which executes you java program. Add this batch file into the registry in such a way that it will be executed during system startup.
simply write following into your batch file
java filename
In linux you will hv to create a .sh script(executable) that will execute your java program.
put .sh in /etc/rc0.d using following commands
cp name.sh /etc/rc0.d/
chmod +x /etc/rc0.d/name.sh