How to run sudo poweroff in java - java

I am a jsp developer. I need to shut down my slow, unresponsive, laggy, (you get the point) development ubuntu "server" sometimes, when its keyboard stops responding to input. I already have tomcat configured properly, and even when my keyboard stops responding, I can use it as a server (accessing webpages served by tomcat). My idea is to make a jsp page that calls sudo poweroff. But I have no idea how to do that. I have tried
Runtime.getRuntime.exec("sudo poweroff");
but it does not work for me. I have already edited the sudoers file to allow the tomcat user to perform this action without requiring a password. How can I make this happen?

If the user running tomcat has sudo perms (w/o a password), it should be doable - but the call should be something like this:
Runtime.getRuntime().exec(new String[]{"sudo", "poweroff"});
The command and parameters can't all be passed in one big string.
Check out the docs for more info: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])
PS: This is probably a bad idea. :)

Related

.bat file not running when executed by app as windows service

So, I have this client-server app which is written in 4th Dimension Language, that runs as a service so it restarts automatically if something happens to the server. This language has a built in function which allows you to run cmd commands and I have another java app in the same folder, that is in charge of sending emails, something my server side app cant handle. We use a command for running this jar from cmd and send the parameters from there, creating and xml for it to create an html from that and send it via email. The thing is when I run this command with the app running as a service, the command simply does not run, but, if I run the app normally, it works like charm, with no problems whatsoever.
At first I thought it could be the paths, so I got all the paths to be absolute, using the full route, yet it doesnt work still. Also I tried exporting the command as a bat and running it by hand, in the exact same path were the server is, and it works just fine. I thought that maybe the service needs some sort of admin privileges, so I started it as Admin from the service, but it changed nothing.
Is there any chance the service has some sort of limitation which doesnt allow the app to execute external commands? If so, is there anyway to bypass this limitation?
Well, I couldn't make it work when it was running by service, so I made a bat with the command the service uses and pasted it in the Windows Startup Folder, so it "opens manually" when windows starts. It's not the real answer, but is workaround for needing to use the service.

Using awt.Robot with Tomcat service

I am trying to simulate user input (keyboard) using Robot library from java.awt, and it is working on my development enviroment, but when deploying to Tomcat (installed as a service on a different machine), it wont work at all. My OS is Windows.
Is there any configuration that I am missing? I already tried "let tomcat interact with the desktop" option, changing users to run the service, changing java used by tomcat (from jre/ to jdk/jre/..), but still I can't make it work.
Also, if it is not possible, is there another way to achieve this?
I finally solved this, not in the best way I guess.
I stopped Tomcat Service, and then started it from CMD as the current user.
Steps to open tomcat from cmd:
Open cmd.exe
Path-to-tomcat\bib\tomcat7.exe
And then opened my browser, went to the application, tried the proccess and the robot worked correctly!
I know this is not the best way as it can led to security issues, but I couldn't make it work using it as a Service because the different user sessiones (Session 0).

Java Security Exception list does not work

I have a problem with adding sites to the java security exception list.
According to the documentation (http://java.com/en/download/faq/exception_sitelist.xml) the exception list does exactly what I want.
Stop this stupid security message and let me use the application which I KNOW is secure.
Following setup:
Ubuntu Development machine, running application server with a little java applet on the login page (Do not ask why, I don't know thats how the application works).
If I use my local browser everything works fine.
But I have to test the site with Windows in an virtual box VM.
I have an IE8/Win7 image from modern.ie running.
I installed the latest java on this VM and every time I try to login the Java applet is blocked because of my security settings (yeah.. "my").
I added the hostname of my machine to the exception list in the java control panel (in the vm of course).
But this does not change anything it.
The application runs at http://myhost:1234/APPLICATION-NAME
I've added every possible combination to the exception list like:
http://myhost
http://myhost:1234 (with and without trailing /)
http://myhost:1234/APPLICATION-Name (with and without trailing /).
nothing worked.
Does anyone know if this exception list thing still works as stated on the FAQ page from oracle?
Why is there not a button named "oracle I know what I'm doing".

Disable message print on terminal when using Desktop.browse

I'm developing a command line tool and at some point it redirects the user to the default web browser. I use the following code for that
if(Desktop.isDesktopSupported()){
Desktop.getDesktop().browse(new URI("http://www.example.com"));
}
The browser opens without any problem but there are some messages printed on the console while this is up. stuff like
[6620:6620:0622/180058:ERROR:browser_window_gtk.cc(1082)] Not implemented reached in virtual void BrowserWindowGtk::WebContentsFocused(content::WebContents*)
or
Created new window in existing browser session.
Is there a way to stop printing these kind of messages. (as it is a command line application it does not look good). Is there any other way to open a browser?
Thanks in advance
This looks like this is printed by the browser on startup. These GTK log messages aren't uncommon.
You can start the browser directly from the console to verify this.
EDIT:
As starting the browser is handed off to an operating system specific method, there's not much control over it.
To have better control you can try to launch the browser directly:
launch the process Launch JVM process from a Java application use Runtime.exec?
for linux (more or less cross-distribution): Linux: command to open URL in default browser
for windows: Launching a website via windows commandline
You can use Desktop.browse if the other methods fail.

Tomcat as service unable to work with system clipboard?

I made a password manager as java web application (Tapestry to be precise) and idea is as follows, when you click on button next to password label, the matching password should be copied to system clipboard. It works perfectly when I start tomcat the standard way (run startup.bat), but that's not what I need. I need my tomcat to start as Windows service at startup, but in this case, everything in my application works perfectly except coping to clipboard. No error occurs, nor I get anything wrong in the log, text just doesn't copy.
Can someone tell me why is this the case, and what can cause such behavior? Can it be that service doesn't have right to mess with clipboard and if so, can I make it work?
Any help is welcome
When tomcat runs as a service, it does not run was a user linked to the main windows GUI, so all the awt functionalities are disabled. The only way to get some of them back is to add
-Djava.awt.headless=true
to the tomcat startup parameters - see http://support.sas.com/kb/12/599.html
However, this may be not enough as each user has his own console, so tomcat will copy into its own user console - you should run tomcat with the same user as the local logged in user, making the "run as a .bat" the easiest option.

Categories