Run .bat as a Windows Service - batch-file

I'm here again with another case that is getting me out my mind.
So, this is happening, I'm trying to run an executable java class(.jar) as a Windows Service, and all my attempts failed so far. To make it a little easier, I turned my jar into a batch file, wich only executes the jar in background, here is the code:
start "" javaw -jar C:\LocalService.jar
The batch works fine. However I have tried to install this batch as a service by using the next line in cmd:
sc create "LocalService" binPath= "C:\LocalService.bat"
The Service installs correctly, but as soon as I try to start it, it pops up an error (The code error is 1053, says something about the service did not start correctly)
Also, I have try with a software called NSSM (non-sucking service manager) It installs fine too, but the service does not start either.
Do you guys know what am I missing?
By the way, I'm doing all this on Windows 7 Professional.
Thanks!

thanks for your comments
Both tools didnt work for me, sadly. However I was able to do it with a software called Java Service Wrapper. This is not a free software, so I needed to buy a license to get it to work.
The steps were simple:
1.-Create a folder with the name of the service, then inside add 4 folders: lib,bin,logs,conf
2.-On the lib folder you have to copy your jar and also the wrapper.jar and wrapper.dll (these last two are in the zip you download from the website)
3.-Copy 4 files to the bin folder: InstallApp-NT.bat.in, App.bat.in, UnintstallApp-NT.bat.in and wrapper.exe (this last one is the one that defines your license, if you can get a file wrapper.exe from somebody else who had paid a license it will work fine. These file also came in the zip from the website) Remove the .in from the batch files
4.-The most tricky step is this: You have to copy from the wrapper's folder called conf a file called wrapper.conf.in Remove the .in extension and edit it with a tex editor. The most important lines you have to edit are:
wrapper.java.command=C:\Program Files (x86)\Java\jre7\bin\java //Specify JRE Path. Will work with eviroment variable
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperJarApp //Choosing this class means your are using a .jar file to execute when the service starts
wrapper.java.classpath.1=C:\LocalService\lib\wrapper.jar //This one is constant.
wrapper.java.classpath.2=C:\LocalService\lib\LocalService.jar //This is the path to your executable jar
wrapper.java.library.path.1=C:\LocalService\lib //Path to your own lib folder (the you created at the begining)
wrapper.app.parameter.1=C:\LocalService\lib\LocalService.jar // again the path to your jar
Then just execute the InstallApp-NT.bat and start the service and your are done
It worked to me with absolute paths, however according to documentation it will work fine with relative path too.
This is how I solved my problem and I hope someone with the same issue will find this helpful
See you next time!

Related

FileNotFoundError: [Errno 2] No such file or directory selenium\\webdriver\\remote\\getAttribute.js'

I'm working with selenium. The script is in :
C:\Users\User\Desktop\Data Analytics Arg\Proyectos\datademia\Py_install\py_ejemplo.py . Venv is activated and chromedriver.exe is in C:\Users\User\Desktop\Data Analytics Arg\Proyectos\datademia\Py_install\chromedriver.exe
The script runs perfectly. Then I created an only .exe-file via terminal :
pyinstaller --add-data "chromedriver.exe;." --windowed --onefile py_ejemplo.py
Folders are created correctly (build and dist). The .exe file (py_ejemplo.exe) was created, but when I try to run it, I get this message:
I've been looking and still can't solve it... I've tried these solutions :
filenotfound
but didn't work for me...Could someone help me? I donĀ“t know what's wrong...
Thanks in advance
I got the same problem but I was working with Firefox and geckodriver.
In my case, I copied the selenium folder from the virtual environment to the dist folder and it worked.
There are a few things you should ensure when packing a script with pyinstaller build with selenium web driver.
It may require to add driver executable when building. I.e. chromedriver.exe
It may also require to add some package files related to selenium such as getattributes.js file when building. It was required at my project.
pyinstaller will extract those files to temp folder in AppData for windows users. So in your code, your relative paths may require to be resolved with a sample function as below (if you are running your code in vs code or you are running through pyinstaller executable the paths should be resolved by function).
For item 1 and 2, you can use --add-binary and --add-data features of pyinstaller for each of them. It is also possible to do this in *.spec file with add-files list, following your first running of pyinstaller (see this explanation) I preferred command-line option as below.
pyinstaller ./app.py --onefile --noconsole --add-binary "./driver/chromedriver.exe;./driver" --add-data "C:\Users\YOUR_USER_NAME\.conda\pkgs\selenium-3.141.0-py38h2bbff1b_1000\Lib\site-packages\selenium\webdriver\remote;selenium\webdriver\remote"
For item 3, to resolve relative paths in your source code, you can use below function in related places (for example when accessing chromedriver.exe)
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
Use above function once you need to access packaged executables and files in your source code. In below example, my chromedriver is inside driver folder in my workspace. But when it is accessed through pyinstaller executable, it will be extracted to temp folder in AppData, yet function will access it through sys._MEIPASS variable set by pyinstaller.
driver = webdriver.Chrome(executable_path = resource_path('./driver/chromedriver.exe'))
Hope it works.

MSDeploy Auto Generated deploy.cmd path issue?

I've been working on trying to figure out why our auto-generated deploy.cmdscripts will not work when installed to C:\Program Files (x86)\OurProgram. I finally narrowed down the issue to the closing parenthesis in (x86) as the script would terminate with "Files was unexpected at this time". One possible solution was to manually create another batch file to set _DeploySetParametersFile to a Windows path ( C:\Program^ Files^ (x86^)\OurProgram\Program.Parameters.xml). This isn't a valid solution for me, so I dug into the auto generated file and found the issue to be 2 variables:
RootPath
_DeploySetParametersFile
Both of which were referenced using %'s(i.e. %RootPath%), however as soon as they were changed to !'s(i.e. !RootPath!), no 2nd script was needed and I can run the cmd script from Program Files (x86) just fine. The issue of course with this is, the deploy.cmd file is auto-generated on each build, thus I have to change it for each build I do. My question is this: Is there a way to edit the template Visual Studio uses to generate the deploy.cmd file? If so, where? It seems so ridiculous that Microsoft would have this be an issue in their web deployment.

Unable to do a simple rename file in ReleaseManagement during deployment

I have a problem that has been bugging me.
I have a Visual studio 2013 solution that goes through Release management.
One of the release steps in Release management is running a simple batch file using the Actions -> SxSBatchCommand tool. This is a custom action that simply executes a batch file and the batch file executes a simple rename that goes like this:
rename "$(currentDir)\project.ev1.app.config" "project.exe.config"
Which basically takes a specific app.config and applies it in the deployed directory as the main config.
All this works well but because I don't like all the batch files in my project, I decided to delete them and to use the WindowsOS -> Rename File or folder tool
So I deleted the SxSBatchCommand and added the "Rename File or Folder" with parameters:
FileFolderName: project.ev1.app.config
DestinationName: project.exe.config
I also tried:
FileFolderName: $(currentDir)\project.ev1.app.config
DestinationName: project.exe.config
This failed during deployment with the message:
The following command failed with the exit code '1' : powershell -command ./ManageWindowsIO.ps1 -Action Rename -FileFolderName 'project.ev1.app.config' -DestinationName 'project.exe.config'
Any ideas why such a simple command wouldn't be working?
Thanks
Can you run this exact command in powershell console ? You can also click on the button next to the command in the release log to grab a better detaild log whith explanation what went wrong .
The file could be in use , or without proper permissions for modification , you can look for the agent's logs in the target machine .
By the way if you are trying to make a file changed to the name of a configuration file for a win service or want to use transformation in a non web application you can use slow cheetah to accomplish it .

Nunit TestResult.xml is not created during Jenkins build

To run tests in Jenkins I use the next batch command:
"C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" /result:TestResult.xml "C:\Users\Denis\Documents\Visual Studio 2013\Projects\MyProject\App.nunit"
Here how it looks in Jenkis:
The problem is: it doesn't generate TestResult.xml file!
When I run the same command as a .bat file from my desctop it creates the TestResult.xml file.
Any ideas what is wrong with Jenkins?
P.S. I searched for created file in all possible folders and even via "search"
I came up with thought that the file is generated and removed then or something like this.
The thing is that I don't see this file indeed but Jenkins generated report based on this file! So I think maybe after generating report the file was removed automatically.
I had faced the same issue and I found the test results xml file under my user directory in Win 7.
Note : I think it's some problem with nunit that it doesn't export the file to the location of which we provide the path.

NSIS simple copy one file from another folder in program files directory to installation dir not working

I tried to create an installer to install a application.
The basic stuff works fine, but I have problems, when it comes up to simply copy an existing files, which is located in some subfolder of Program Files folder.
The scenario behind this, is that the application has different versions and a license file.
When the app gets installed, the installer looks for an existing file, and if exists, should just copy that license file to the $INSTDIR/license folder.
Example:
C:\Program Files (x86)\MyApp\Ver1.0\license\ // here's the location of old license file
C:\Program Files (x86)\MyApp\Ver2.0\license\ // here to put the old license file
Following section is condensed to the relevant stuff:
Section "License of other Version"
IfFileExists '$PROGRAMFILES\MyApp\Ver1.0\license\some_license.slc' beginLicense endLicense
Goto endLicense
beginLicense:
MessageBox MB_OK "We have found an old license file. Do you wanna to use it for current installation ?"
CreateDirectory "$INSTDIR\license"
CopyFiles "$PROGRAMFILES\MyApp\Ver1.0\license\some_license.slc" $INSTDIR/license/some_license.slc"
endLicense:
MessageBox MB_OK "There were no license found."
SectionEnd
The compiling runs fine, but the installation process denotes an error when trying to copy the file. But it gives no further explanation, why it failed.
I tried also using the File directive, but that produced other errors, because of using the constants in file directive (file not found, and that sort of errors) and even the script cannot compile.
I tried also using a macro, mentioned here in another question of stackoverflow (sry, didnt have the link anymore), but it didnt worked for me, too.
What am I doing wrong, is it not allowed to use constants in CopyFile directive ?
Any help is appreciated.
You need to change:
CopyFiles "$PROGRAMFILES\MyApp\Ver1.0\license\some_license.slc" $INSTDIR/license/some_license.slc"
To:
CopyFiles "$PROGRAMFILES\MyApp\Ver1.0\license\some_license.slc" $INSTDIR\license\some_license.slc"
NSIS is not a fan of / slashes. While most of the low-level kernel32 functions can handle them, not all things in the shell can and that probably includes SHFileOperation used by CopyFiles. Even if that was not the case, internal path processing in NSIS tends to eat / slashes...

Resources