Running jar in java using a specific user - java

Let me explain the problem:
Context
I have this application that wants to update. It call an updater jar that I have also made. The updater kill the application running the old version and the updater starts up a new one with the new version.
Problem
The new version doesn't start unless I run the jar my self. Adding a sleep didn't solve the problem.
Hypothesis
Maybe the user running the updater app is java and it doesn't have the priviliges to run the new version
I run the first java with my user than this java application run the jar with "java user" and java user can't run the new version.
Is there a way I can specify a user when I run my jar with the runtime.
Runtime.getRuntime().exec("C://Temp//myapp.jar");

In case you are using Windows environment, trying following might help your cause
try the command on the windows system32 path
runas /showtrustlevels
You should get something like the following:
C:\Windows\system32>runas /showtrustlevels
The following trust levels are available on your system:
0x20000 (Basic User)
You would then take the value for "Basic User" and run something like the following to start jar:
runas /trustlevel:0x20000 "java -jar yourfileName.jar"
or you could try this link it might be useful

Related

Running a Java/Selenium program on another computer

I have a small Java program that uses Selenium that I'd like to install on someone else's computer so they can use it too. It uses Selenium (for what it's worth).
I exported from Eclipse to a jar file. I then used launch4j to create a windows executable. I used Java version 1.7_079 to develop the program.
The other computer has JRE version 1.7_079 installed I made sure CLASSPATH is set.
If I run this executable on my system it works fine. If I try and run it on another system nothing happens at all, no errors, no nothing.
What am I doing wrong?
Would it be easier to NOT wrap it in an executable and just use a batch file to run it?
what's the best and easiest way to accomplish this?
Try to execute the .jar directly on the system where it doesn't work by using java.exe/javaw.exe and note the error, if any.
java.exe -jar helloworld.jar
If you get "'java.exe' is not recognized..." you need to add the folder where java/w.exe is to your path (eg: SET PATH=folder-with-java-exes;%PATH%
You can locate java/w.exe files with:
cd /d c:\
dir /S java*.exe
I finally have an answer to this. I traced it down to an issue with Chrome and Chrome driver. I was running Chrome v55.0.x. The users computer was running 57.0.. Once I upgraded my machine to 57. it failed like the users did. I updated Chrome driver and everything works as expected.
you can try this in your code put the path as C:/xxxxxxxx.exe
and put selenuim and the web driver In the C drive and create a jre or exe now .
next step is to ask your client to put selenuim and the web driver also in the C drive in there computers and int will work fine .

"is not allowed for assistive access" error when running AppleScript from Java

I have a Jenkins config that executes Java tests for OS X app using Maven. And when I'm trying to run some AppleScript that requires assistive access for it, different errors appear. So I've tried to execute AppleScript in two ways:
Using ScriptEngine:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");
engine.eval(script);
In this case script execution fails with error: An error of type -10810 has occurred.
Calling osascript. In this case error looks like: System Events got an error: osascript is not allowed assistive access. (-1719)
In accessibility list I have following things: SystemUIServer, Script Editor, osascript, java, mvn, bash, Terminal. When running script in Script Editor or in osascript from Terminal, script passed without any errors.
Any suggestions what else should I check?
I think you answer your own question. You can allow assistive access for Script Editor (among other things). It works fine in Script Editor.
You can try and add other applications to have assistive access. This might be an option if Script Editor does not give you want you want.
I find that I sometimes need to add the parent of the process that calls osascript, in addition to osascript itself, regardless of what the error message says. Sometimes I have to add the grandparent process too.
In particular, /bin/bash does not call any assistive API, but OSX wants it listed even though osascript is also listed, and I've had to add TeamCity's agent.jar too.
So try adding your .jar file, and look to see whether ScriptEngine uses an intermediate code executor that you'll also need to enable.
Beware that you'll probably have to remove and add your entries if you change the binary (I think OSX maintains some sort of checksum in the final binary entry in its database entry).
Another workaround is to write the AppleScript using Script Editor, save it as a standalone app from there, and give that app assistive access. Then you can run the app as needed from another environment like a Java app or Automator workflow.
See also: https://apple.stackexchange.com/a/346306/264883
I've resolved something like this problem before by inserting osascript into the TCC.db using tccutil.py
After installing I linked tccutil.py into the path.
sudo ln -s /source/tccutil/tccutil.py /usr/bin/tccutil.py
ls -l /usr/bin/tccutil.py
lrwxr-xr-x 1 root wheel 26 30 Dec 14:57 /usr/bin/tccutil.py -> /source/tccutil/tccutil.py
Then to allow osascript access to control your computer use the following compand
sudo tccutil.py --insert /usr/bin/osascript
I even added the script when the problem happened again later on.
sudo tccutil.py --insert $(pwd)/transcribe-audio-to-text.js
That failed so I rebooted which worked :-)
transcribe-audio-to-text.js currently uses System Events to make TextEdit be able to save a text file. It's part of virtual assistant which is very alpha at 20160105 but the source is interesting. virtual-assistant

Running a Java Application on a Shared Drive from Windows Task Scheduler Client Side

First, just wanted to thank everyone in advance for helping with this, let me take you on my journey...
Task: Run a Java application that is located in a folder on a file server using Windows Task Scheduler, simple enough.
The caveat, is that I would like to utilize the task scheduler option of "Run whether user is logged on or not." and this seems to be a tricky function...
I have set my account as the account that the task is to run under.
What have I tried?
Creating a task based on these actions:
Java.exe -jar "path to share jar"
Java.exe -jar "path to local jar"
Javaw.exe -jar "path to share jar"
Javaw.exe -jar "path to local jar"
All have had the same result with the "Run whether use is logged on or not." option : 0x1
Next, I tried creating a batch file to run this on my behalf and changed the task to run that batch file and I got all the same results. I have read that utilizing the "Start in (optional)" field of an action may solve the problem, but it did not.
After that, I created a .vbs file with code similar to the following:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
This achieved a 'silent run', but task scheduler still did not run with the "Run whether use is logged on or not.", and ended in 0x1.
All of these work fine with the "Run only when user is logged on." option.
The reason I want to do it whether I am logged on or not, is because I want to run my application before I come in to work.
Update:
I've disocvered a few things. In power options for Windows, I had to enable the ability for scheduled tasks to wake the computer, felt kind of dumb about that one.
Using a simpler task - one that just creates a blank text file - this option works fine. I belive the problem lies with executing Java.
The task needs a user to run. It doesn't have to be the user that is logged in, of course, but a user is needed anyway.
Make a batch file (or equivalent) that works when you launch it manually
Schedule the same batch file (with cmd /c c:\full\path\to\batch.bat) to run as yourself, with your password and everything.
Now you are ready to debug your problem
The user Windows will use to run your task when no one is logged needs all the tools configured and working. For exemple, does local service have all the path and registry settings set ? Is the share readable by that user (the SYSTEM account does not have network credentials).
To help in debugging, use psexec. This command line will allow you to run your script under the SYSTEM account. Something like
psexec -dies cmd /c c:\full\path\to\batch.bat
+Welcome to SO !

Wrapping a jar file in a Windows service

Have you had experience with running a jar file using a command line, wrapped in a Windows service?
I'm trying to find a way to run a jar file without being logged into the machine, and since it allows command shell, I was wondering if it's a good idea.
Thanks!
Original Post:
I'm trying to run Associated Press's Web Feeds Manager, which is basically a jar file that can be run when logged in by double clicking it.
I'd like to run the same file but without being logged in to the machine. In their manual (http://wfm.ap.org/admin/content/help/Running_Agent_on_a_Remote_Server.htm) they write how to do that, using a commandline parameter.
Basically I'd like the jar to run as a Windows service, regardless of who's logged in, but Googling it showed it was problematic.
Have you had experience with remotely running jar files? What are the pitfalls?
Thanks!
On a google search, I came across this article -
Running Jar Applications as a Windows Service
It mentions about open source Java Service Wrapper project from Tanukisoftware.org for accomplishing this task.
Note: I've not used this personally.
If you are not interested in having the service started/stopped at boot/shutdown, but you just want the program to be started manually and keep running after logout, here is what you do:
$ nohup java -jar foobar.jar > foobar.log 2>&1 &
which means: start my foobar.jar (java -jar) and keep it running after I logout (nohup) redirect stdout to foobar.log (>) and also the stderr (2>&1), and make it running in background (& at the end).
Instead, if you are interested in installing a "service" in your linux box, there are many options, depending on what distribution you are using.
The most common are upstart (for ubuntu) and System V init scripts (Redhat or others). Also cron can be used to start/stop services at startup/shutdown.
You can find an example of installing a java app (hudson) on an init system here, or doing the same thing with upstart. Or, as I said, cron could be an option.
On Windows, there is Java Service Wrapper. And not much more.
For windows Java Service Wrapper is a better choice
My favourite is the upstart on linux, but it is Ubuntu only.
On Windows I see many alternatives according to this forum.

How do I run a Java .jar file as a Windows service on Windows Server 2008?

How do I run a Java .jar file as a Windows service on a Windows 2008 server? I have a jar file called SomeJavaFile.jar located under the C:\SomeDirectory directory on a Windows Server 2008 box. I usually just run this jar manually in the command line with: java –cp SomeJavaFile.jar com.ctg.SomeJavaFile and I let it run 24/7.
The problem with our Windows Server is if it restarts I need to run it as a service so it will automatically start as a service upon startup, because our processing for Vistakon runs 24/7. I have Googled around and tried to use a service wrapper and the sc.exe command to create the service.
I put the wrapper service.exe in the C:\SomeDirectory\. I use the following command to create it: sc.exe SomeJavaService binPath= “C:\SomeDirectory\service.exe \”java –jar C:\SomeDirectory\SomeJavaFile.jar\”” type= own start= auto error= ignore. This creates the SomeJavaService service correctly but when I try to start it I get an error that says the service on Local Computer started then stopped.
Some services stop automatically if they are not in use by other services or programs. Do I need to alter my sc.exe command to have the exact previous working command line maybe, by adding in the com.ctg.SomeJavaFile? So should I change This jar should just run in the background and constantly poll/ping the C:/poll directory then if there is data present it processes the data and sends an export file to another directory.
I have used this same .jar file for years successfully and it hasn't changed, but I cannot get it to run as a Windows service. This is the site I use to get the service wrapper http://code.google.com/p/simple-service-wrapper/. Any help would be greatly appreciated!
"winsw" is the standalone version of the Windows Service installer shipping with the Glassfish Java EE reference implementation.
Works well, but is not a fully polished product - I have used it for exactly this purpose for a couple of years. Requires .NET in some recent version on the Windows machine.
https://github.com/kohsuke/winsw
I think that the best bet would be wrap your java app with Procrun of Apache Commons Daemon .
Procrun is a set of applications that allow Windows users to wrap
(mostly) Java applications (e.g. Tomcat) as a Windows service.
The service can be set to automatically start when the machine boots
and will continue to run with no user logged onto the machine.
As per my analysis,
The Idle Solution will be writing a VC++ (.net) Windows Service creation program to launch the .bat (that triggers the jar file)/.exe as a System service with all the required call back methods to SCM.
Note : 1. Wrapping the process with sc.exe / srvany.exe would not work as it does not have any call back process to the SCM (Service Control Manager). 2. And java service Wrapper is a third party API (many are LGPL licensed).
If you start your Java code from commandline by using java -j some.jar does it run until you terminate the program, or does it stop by itself?
There needs to be a NON-Deamon Thread, that is running all the time. A JVM will terminate, if there is no thread running, that is not flagged as daemon.
If you have a little budget, buy an installer tool. I use instll4j. With that tool, you can create service launcher and install them during instllation.
The following solution is working fine for me having OpenFire Connection Manager (which is a jar file) running as service on Windows Server.
Download and install the Non-Sucking Service Manager
I didn't use the batch made by OpenFire team, because it didn't work for me (dependencies not found error...) So, make a batch file with the following code :
#ECHO OFF
cd /D "E:\connection_manager\lib"
java -jar startup.jar
and save it as cm_startup.bat in the bin folder of connection manager.
After that you can create the service with NSSM.
So, open a cmd.exe and run the following command :
nssm install ConnManager "E:\connection_manager\lib\cm_startup.bat"
.
Doc & examples
More documentation and examples for the Non-Sucking Service Manager here : https://nssm.cc/usage Actually NSSM as a lot of options available.
Here is a more complexe example :
nssm install solr "%JavaExe%" -Dsolr.solr.home="\"%CD%\solr"\"
-Djetty.home="\"%CD%"\" -Djetty.logs="\"%CD%\logs"\" -cp
"\"%CD%\lib\*.jar"\";"\"%CD%\start.jar"\" -jar "\"%CD%\start.jar"\"

Categories