Disable message print on terminal when using Desktop.browse - java

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.

Related

How to run sudo poweroff in 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. :)

Runtime.exec not showing GUI in JBoss environment

There are many Q & A on Runtime.exec, but still I found a strange behavior. I am running desktop application in which jobs (a kind of javax.resource.spi.work.Work) are submitted and executed in JBoss application server. Following is the code to run some script, which is blocking whenever it needs to launch any GUI. For example, if the script or command is
start notepad
The GUI is not launched, but i can see the notepad.exe in Task Manager. I dont know what is blocking to launch the notepad GUI. Following is the code:
String pathString= "D:\\folder\\abcd.bat";
pro = runtime.exec(new String[] {
pathString
});
Content of abcd.bat file is
start notepad
No error logs or exception found. The above code works very well in normal Java class.
EDIT: Issue found only with .exe files which launch GUI. Some .exe which does not require launching GUI but running some background task is executed very well with my code.
EDIT: Forgot to say that my application is running as service.
Starting from Windows vista, services not allowed interacting with desktop. More details here . It is windows security restriction to not allow services to interact with desktop. I have done changes in regedit and Log On properties of my service, it worked well.

Launch an Application from a Browser

I would like to have a link or button on my web site that launches
vim (an editor) on a specific file (e.g., myfile.txt) on my local machine.
I want to launch C:\Vim\Vim74\gvim.exe on C:\Users\paulco\myfile.txt form any browser.
I want this to work on all (realistically most) browsers.
I actively use Chrome, Opera, FireFox and IE (in that order of preference).
In order for it to work across all of these browsers,
I think the script has to Java-based.
Does anyone know how to do this?
Does anyone have a Java-based script that does this?
Here are some resources I found on the topic.
But either they are IE specific or don't work.
Launch application from a browser
http://msdn.microsoft.com/en-us/library/aa767914%28VS.85%29.aspx
I have tested the following on Internet Explorer, Chrome, Opera and Firefox.
The browser has to run on a Windows operating system, Linux and Mac require different aproaches.
Solution is to define a protocol handler for a custom protocol.
1) Take this HTML example, it should open the specified text file using notepad.exe:
Open Textfile
2) You need to define the protocol handler in the windows registry, save the following to a file named testing.reg and execute it (double click on it), or enter the values manually using regedit:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\test]
#="URL:Editor test"
"URL Protocol"=hex(2):00,00
[HKEY_CLASSES_ROOT\test\DefaultIcon]
#="\"C:\\Windows\\system32\\notepad.exe\""
[HKEY_CLASSES_ROOT\test\shell]
[HKEY_CLASSES_ROOT\test\shell\open]
[HKEY_CLASSES_ROOT\test\shell\open\command]
#="\"C:\\temp\\editor.bat\" %1"
3) As you can see, I am not calling notepad.exe directly, but a c:\temp\editor.bat batch file. This is because the file-parameter has to be modified. using the %1 as a parameter will pass the complete URL including the test: custom protocol name to the shell. In the script, I use a simple substring pattern to extract the specified file name and call the editor:
REM editor.bat
set param=%1
notepad %param:~5%
That's all!

How to run external application from a Java EE web application?

I have been working on a Web Application for Intranet use only. I work with Apache 7 and Windows Server 2003.
In one of my page, I need to open an external application that we can locate in C:/Program Files/etc... with some parameters already initialized. Of course, the user has to use the web application on the server to make it work (that will be the case).
To open the application, I use the ProcessBuilder object.
The problem:
When I work locally with Eclipse and run the server by hand, it works perfectly. Any application can open itself from a web page.
But when I use the tomcat windows service (and that's what we want to use on the server), it just never launches. Or to be more specific, it launches and stops the application directly. No java errors thrown and process.waitFor() with an exit value of 0. The fact is that we can run processes via the web application (I tried to run simple batch files), but when there is an UI involved, it will never appear.
Again, on the server this time, if I launch tomcat7.exe (that we can find in the %CATALINA_HOME%/bin directory) with a double click or cmd, the UI in the web application will appear. If I launch it with services.msc or tomcat7w.exe or tomcat7 start via cmd, it will not.
I thought of several things:
use another user to start the service
change the way the service is launched (StartMode: jvm, java. I did not succeed with exe)
I read Tomcat 7 Windows Service How-To many times but didn't find out anything to solve my issue.
Have you any idea of what is happening, and how to solve this issue ?
What is the big difference between running tomcat as a service and from the command line?
Option 1
If you open your service's properties window, go to the Log On tab then check the "Allow service to interact with desktop" check box you will get the behavior you want. Also depending on what app you what to run you may need to change the log on account.
see Launching GUI App from Windows Service - Window Does Not Appear
Option 2
Did you try start a cmd and there use
start /c "c:\path to\exe"
in cmd.exe type
help start
Option 3
You will need a daemon service that is not run as a service. windows puts certain restrictions on service apps.
This sleeping app can be started by tomcat or other your self. it can listen on a port or poll a folder for a new file, and when it gets a job to do it starts the app you want. Via port or text file you can send the parameters.

Open html in browser from Java. Bug if browser is not started?

I am using
java.awt.Desktop.getDesktop().browse(uri);
to show my users a generated html file. All is fine if the browser (firefox 3.5.7; linux) is started before I make the call. But if the browser start is triggered from the getDesktop().browse call then the java application will not exit until the browser closes.
How can I avoid this behaviour? Is this known under windows/macOsx too?
(If I then force the exit of the java application the browser will close too and sometime even crash!?)
The reason the browser exists is because the browser is launched as a dependent process...so when the first process shuts down, all its dependent processes are shut down with it. But in the case when you start the browser first, it already has a different process ID that isn't affected by your application. I think the only way to avoid this behavior is to use a different technique (perhaps Runtime.exec()?) to launch the browser in a way that registers it as a non-dependent process.
The project Browser Launcher (http://browserlaunch2.sourceforge.net/) can be the solution for your problem. You can launch a browser from your code like this:
String url = "http://....";
BrowserLauncher() launcher = new edu.stanford.ejalbert.BrowserLauncher();
launcher.openURLinBrowser(url);

Categories