I made a batch file that executes commands through a jar:
#java -jar commands.jar %*
This is the file tree:
C:\
Commands\
commands.bat
commands.jar
I have my PATH variable set to "C:\Commands\" so that I can access the commands from anywhere on the PC. I can successfully run the batch file, but for some reason it won't open the jar. When I type
commands
or
commands help
it says
Error: unable to access jarfile commands.jar
I have tried using quotes around the jar name, I have tried moving the jar... nothing works...
%~dp0 gives bat file path so
#java -jar %~dp0commands.jar %*
See call /? for help (even though we aren't using call).
By using ftype, assoc, and editing with setx the variable pathext to add jar files you can just type the jar's name and have it run without a batch file needed.
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
I have a runnable jar-file which I want to start from a batch file. However the jar-file has to be started with a VM-option. The following batch file starts the jar file (in a static way).
java -Djava.security.policy=C:\Users\uname\
\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://C:/Users/uname/Documents/Folder
/anotherFolder/target/classes/ -jar %~dp0jarfile.jar %*
pause
btw: I know that
\src\main\java\rmi\client.policy
is not in a jar file yet, but I'm assuming that everybody has this file structure already on his machine.
However, I want to be able to start the jar file with a relative path, so that every Windows10 (x64) user can use my jar file system-independently. How to achieve that via batch?
Replace each reference to user home C:\Users\... with %userprofile% variable as per this answer explanation.
java -Djava.security.policy=%userprofile%\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://%userprofile%/Documents/Folder/anotherFolder/target/classes/
-jar %~dp0teamFour-1.0-SNAPSHOT.jar %*
or switch to %userprofile% directory with cd before executing java and depend on relative paths.
I am having various .jar files in my system.
I have the JDK and JRE installed.
Most of jar files run on double clicking, but there are 2 - 3 jar files which do nothing on clicking. Help me.
By the way I am using windows 10 64 bit
Problem:
I could not start JAR files just by clicking on them in Windows 10.
I was messing around with the "control panel" ... forget it.
What I've found out:
Start a command line (cmd) as an Administrator
Check your file type association:
ftype jarfile
assoc .jar
I had to change my java path to a different one
ftype jarfile=C:\myjavapath\javaw.exe -jar "%1" %*
Which basically means, that if someone starts a jar file, the command would be:
C:\myjavapath\javaw.exe -jar "example.jar" parameter1 parameter2
For me, I also had to change my .lnk files to only contain the name of the jar file, not the whole command. Type in the whole path of the jar file in the "target" field in the properties of the link file (.lnk).
You can debug it in the Command Prompt.
Open start, type in CMD.exe, hit enter
Then, type in
java -jar "path\to\file.jar" without the quotes
or
java "path\to\file.jar"
You should be able to see an output log of what is happening that is making the jar file not execute properly
Your Manifest file should contain a line:
Main-Class: classname
See "Setting an Application's Entry Point".
I have a .bat file which is my .jar file launcher. The .jar file is a native application that talks with a Chrome extension. I have the following .bat file:
#echo off
rem set JARFILE = %CD%\MyNativeApp.jar
java -jar C:\Users\reza.ahmadi\workspace\SimpleNativeApp\bin\MyNativeApp.jar
pause
The .jar and the .bat files are in the same folder (along with my manifest). It works well.
My problem: It does not work if I replace that path there with just the file name, meaning:
java -jar MyNativeApp.jar
does not work for me. I am working with a Win 8 machine.
Any comments would be appreciated.
You can dynamically substitute the path by getting it from the currently executing batch file (%~dp0):
java -jar "%~dp0MyNativeApp.jar"
If the jar file is in the same folder that the batch file, you can use
#echo off
setlocal enableextensions disabledelayedexpansion
for %%a in ("%~dp0\MyNativeApp.jar") do set "JARFILE=%%~fa"
java -jar "%JARFILE%"
pause
You can find here more information on how to retrieve the current active folder or the folder where the batch file is stored
I have a jar file named test.jar, which I am running with a batch script from the same folder.
Here's the batch code:
java -jar test.jar
pause
The jar itself works with no problems, and I can run it just fine.
However, if I try to run the batch file as an administrator (by right clicking it and choosing "Run as Administrator"), I get the following error:
Error: Unable to access jarfile test.jar
I'm using Windows 8.1, but this also happened on a machine running Windows 7.
What can I do to be able to run this as an Administrator?
i had the same problem that you and i solved it by changing
java -jar test.jar
to
java -jar %~dp0test.jar
%~dp0 holds the directory of your bat file (AFAIK) so %~dp0 will give Java the full path to the jar file solving the problem.
You could also ad a temp path to java
path=C:\Program Files\Java\jre1.8.0_40\bin
Java script
path=