The Java code runs the command prompt and passes parameters to it, and then executes. it works fine in Eclipse IDE, but when I make it as a service (in Windows 7) and run it, it doesn't work. What I want to ask is will this service invoke the command prompt, pass parameters, and run it. Just for reference on line code is given below.
String status = WMI.execute(new String[] {"cmd.exe", "/C", "cscript.exe", vbScriptFilePath, ipAddress, username, password, service}).split(WMI.CRLF)[0];
This is windows 7. It may have user access control enabled.(why not?).
In that case starting service is only done with process with administrative permissions. The user being administrator is not enough. See this question/answer and comments
Run java application as windows service (using jsl) - get error when installing
Related
So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.
I have a Java application that runs on a Linux server and performs series of actions (create directory, copies few files, checks if an application is running etc). I am using log4j2 for logging messages and it has been running fine.
I've a requirement to read an account and impersonate as that account (passwordless sudo switch user) and then perform series of above actions. Access to do a passwordless sudo has been taken.
I've already done this using a shell (first switch user and then execute the Java program) however I've raised this question to understand if this can be done within Java program ?
Execution: java -jar xyz.jar parm1
To evaluate, I changed the Java code to run a shell command as below (I am using processbuilder):
When I run the Java program, the logs appear only till the point of sudo shell command execution and no logs are shown post that step.
Is there a way I can retain all logs before and after switching user (impersonating) ? (I'm using log4j2 and writing to a log file which has write access to other users).
Please share if there is any known solution ?
[EDIT] Adding java code snippet used to trigger sudo
public boolean switchUser(String account) {
ShellCommand shellCommand = new ShellCommand();
List<String> cmd = new ArrayList<String>();
cmd.add("su");
//....I construct asu command for the passed account here. I have passwordless sudo su access and tested fine manually
//I've some logger messages here
shellCommand.runCommand(cmd); //Class with method that uses ProcessBuilder and executes shell command
}
[EDIT] I think when I trigger su from Java it opens a new shell while Java is waiting for it to complete. This is possibly the reason of no logs visible there after. So one solution will be to Wrap it in a shell, do a su with command to run the jar.
I am creating a hotspot software for which I need to run a batch file as administrator from a java program. The batch file contains the following two commands:
netsh wlan set hostednetwork mode=allow ssid=name key=password
netsh wlan start hostednetwork
name and password are taken as input from the user.
As capturesteve has written - run the app as administrator. You can use for example cmd.exe under Windows. If you run the cmd.exe with administrator privileges, everything what will be started from this "administrator" cmd.exe will inherit the rights of his owner. It's a universal principle in Windows (and not only in Windows).
Run the cmd.exe as administrator and start the java app from it:
https://technet.microsoft.com/en-us/library/cc947813%28v=ws.10%29.aspx
http://www.softwareok.com/?seite=faq-Windows-8&faq=7
+
Java: run as administrator
Executing Java program as administrator
The best way would be to run the java application itself as sudo.
Is there a way to let a tomcat 7 server (running on an Ubuntu Server) execute a sudo command in a command line?
In this specific case I want to make it possible to shut down the system from another server.
What code would I have to use?
And do I have to run the tomcat server as root or can i get these privileges at runtime and only for this action?
Thanks for your help:)
No need to run Tomcat as root, you can just add tomcat user to /etc/sudoers to allow him run superuser commands, like sudo shutdown now. This way allows you to specify desired subset of commands that can be executed by user. E.g. to allow him to run only shutdown:
tomcat ALL=NOPASSWD: /sbin/shutdown
To run shell commands from Java code you can call one of Runtime.exec() or use ProcessBuilder.
P.S. Also try googling about /etc/shutdown.allow file which allows running shutdown command by any user that is listed in it. But I've never used it.
I have a dos command which starts an application. I want to start the application as an administrator. How can i do so? Below is the code which i tried.
String arg[]={"C:\\app1.exe", "C:\\app2.exe", "c:\\app3.exe"};
String pwd[]={"123","-x","-sf"};
String outputfile="c:\\output.xml"
String command=arg[0]+pwd[0]+arg[1]+pwd[1]+arg[2]+pwd[3]+output;
Process pr=rt.exec(command);
I tried the same command from command prompt and its working fine. But when i try to run the same from java code, it keeps on running without producing any output.
see runas -command:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx?mfr=true