Fake X11 display? - java

I have a Java program using AWT which I would like to run on a headless system. The display for the program does nothing other than display stats. When the program finishes, it exits. There is no user interaction on the display. The program creates an output file which I use in my build system.
Is there a way to get the Java program to run without an X11 display configured? Can I force Java to run the program without trying to display anything? I do not have access to the source code (it is just .jar file), so I can't make modifications to the source.
Any thoughts on how I could get this to work?

The underlying question here is how to run Java applications without an X server; providing a "fake" X server is only one option. In Java 1.4 and up, you can do the following:
java -Djava.awt.headless=true
This allows applications which use AWT to run on headless systems even without an X server.

Xvfb can do what you ask for. I've not used it myself, but here is a link to wikipedia: http://en.wikipedia.org/wiki/Xvfb

You can use a vncserver.
vncserver :1001
export DISPLAY=localhost:1001
java..
The added advantages is that you can actually view the gui
using vncserver 'just in case'

Could also run Xvnc in a low resolution and color depth.

As mentioned by Charles Duffy the traditional method is to tell Java to go headless.
Note that you can always mount the jar in Eclipse and use jad+jadclipse to see what it actually does, and perhaps even override a class if you need to by putting another class-file in "front" of it in the classpath.
A facility that might be relevant if the program uses Java2D is that newer Java versions use optimizations in the X11 server to render faster. This alone might be a reason to devote an X11 server attached to a high performance graphics card to your graphics processing.

I've used with great success in the past the PJA libraries, they don't seem to be maintained anymore, but then again, just just want to run...

I was able to get headless mode in OpenJFX with the command line arguments
-Dglass.platform=Monocle -Dmonocle.platform=Headless -Dprism.order=sw

Related

Java IDE for working over Remote Desktop

I have recently found out that anything Swing(NetBeans, IDEA) is excruciatingly slow to paint the UI over Remote Desktop(RDP).
Can you guys give me any suggestion for something that will work properly over RDP?
Actually, contrary to everything I've seen - mostly the "Dsun.java2d.noddraw=true", which is mentioned in a number of places, it is actually setting it to false that fixed the drawing issues(for me at least). Go figure.
RDP is optimized for native Windows apps and, regrettably, newer versions of RCP (like Vista/Win7's default RDP) is even more hostile to non-native apps than older (XP/Server 2003) versions. Here's a good link:
http://devnet.jetbrains.net/thread/280673
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4204845
One workaround is to get a screaming fast CPU/graphics board, tons and tons of memory and a super high-speed network connection :)
Another is to:
Select "32-bit true color" in your Terminal Services settings
Select "Modem"
Reduce screen resolution (heck, use 1024x768 if possible)
Here are some additional tips:
http://www.codinghorror.com/blog/2006/04/remote-desktop-tips-and-tricks.html
Try setting "Dsun.java2d.noddraw=true"
http://ubuntuforums.org/archive/index.php/t-1129187.html
http://www.mindfiresolutions.com/Solving-DIRECT-DRAW-ddraw-problems-in-Java-Swing-730.php
I am launching a java swing application from Linux, and display it as a Windows frame thanks to MobaXterm.
If you haven't done it already, please have a look at the following documentation :
https://docs.oracle.com/javase/8/docs/technotes/guides/2d/flags.html
Using -Dsun.java2d.noddraw=true did not fixed my issue, but setting xrender to true did.
So here's the code I used on Linux :
export _JAVA_OPTIONS='-Dsun.java2d.xrender=true'
java -jar my_java_application.jar
Maybe it's not only Swing that paints slow over Remote Desktop. However, Eclipse is based on SWT, based on native widgets, so it is not Swing.
You should give JavaWIDE a try. It is an IDE designed for access to a JDK without it installed on a system, with a built-in storage system so you just need access to the internet. It's not exactly a program for accessing everything on the computer but it will do a good job with your programming. It may take a while however, as the compiling is done on a separate server.

Java on Linux: maximize a non-Java GUI application

From Java code, is there a way to maximize the window of a different GUI application? I have access to Process objects created for these other programs, as well as a semi-reliable way to get their PIDs and a generic String indicating the name of the process binary (e.g. "firefox").
I can also programmatically execute full bash shell statements (including commands connected with pipes), if there's some command-line way of going about it.
On MS Windows, I recall seeing somewhere about a Java library that wraps the win32 windowing API, allowing one to pass those Windows-specific signals to applications - would there be something similar to that on a Linux setup? This is for a Red Hat system, if that matters.
Not in a "standards-based" way, no.
The X-Windows system is independent of specific window managers, as such, there is no standard way to "maximize" a window. It ultimately depends on the features of the window manager in use...

Can I use java.awt.Robot from within a daemon?

I have written a server in Java that allows clients connected to it to control the mouse and keyboard of the computer. To do this it uses the java.awt.Robot class.
I need this server to run in the background and start automatically. The first OS I am tackling this problem on is Debian based (Ubuntu 11.04) and a daemon seems like the obvious choice. The problem is that when the daemon is started during boot or during the installation of my debian package (whose postinst script starts it using /etc/init.d/pc-remote-server start) I get this error:
java.awt.AWTException: headless environment
at java.awt.Robot.<init>(Robot.java:97)
at com.se.pcremote.server.CommandExecuter.<init>(CommandExecuter.java:72)
at com.se.pcremote.server.PCRemoteServer.<init>(PCRemoteServer.java:215)
at com.se.pcremote.server.PCRemoteServer.main(PCRemoteServer.java:122)
Is there any way I can use the java.awt.Robot class from within a daemon process? Could I spawn a secondary process from the daemon process that is not a 'headless environment'? Or is there a better way for me to get a 'service' like result that does not have this limitation?
"Headless" means that this code needs access to a graphics environment, and it hasn't.
You can run in headless mode by supplying a system property which provides a crude implementation which gives just the basics for running applications, but which most likely cannot support Robot. Try it however first.
If you cannot do that, you need a graphics environment for your process. The usual way to do this is to run a VNC X-server as it doesn't require physical hardware, and then connect to it.
I assume, you must set the DISPLAY variable correctly (in the environment of the robot process at the time when the robot process is started) for this to work -- in your case you would need to specify a display in your DISPLAY variable which is created some time after the program is started. --
No idea whether this really works, but you could give it a try and report back here whether it works.
Alright, after doing some more research and trying some more options here is what I came up with:
Can I use java.awt.Robot from within a daemon? No.
Further down in my question I elaborated a little:
Is there any way I can use the java.awt.Robot class from within a daemon process? No. As above.
Could I spawn a secondary process from the daemon process that is not a 'headless environment'? Not that I could figure out. It was going to be a lot of work if I did do it anyway.
Or is there a better way for me to get a 'service' like result that does not have this limitation? Yes! Use the desktop environment! In my case since I was using Ubuntu the desktop environment was Gnome. Gnome has a Startup Applications feature that runs off .desktop files on a global and per-user basis as described here. They also provide information on the structure of these .desktop files here. I added a .desktop file to /etc/xdg/autostart (the global autostart folder) that ran my Java 'service' and it worked like a treat.

Java application launcher

I have written a very simple Java application. Can anyone tell me how to create a launcher like icon to run that application both in Ubuntu and Windows ??
Thanks in advance..!!
An executable JAR should work fine for a launcher on both Windows and Linux. However, that won't get you a custom icon.
On Windows, you can use JSmooth, which will create a .exe wrapper around your JAR file. The JSmooth program will let you create an icon for the EXE as well (it also has options such as getting the user to download the necessary version of the JVM, or only permitting one instance of the program to run).
If your java application is to be distributed from a web server, you should have a look at Java Web Start which can do what you ask for based on a JNLP-file. Notably see
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/javaws/developersguide/faq.html#104
Ubuntu and Windows will both have different ways to launch the application. I see two routes to follow here. One is to use Java Web Start and use a web interface to start your application. The other is to search for some sort of 3rd party installer that will create setup/installation programs for all the platforms you want to use.
I actually used a third party install program before, InstallAnywhere, but it was almost ten years ago. It offered the functionality you are looking for, though.
There are different ways to do this, sometimes the simplest is the best approach. One solution as suggested is a simple starting script. Roll your application into a jar, then include a script that does nothing more than "java -jar myscript.jar". I do this all the time for internal customers that may be running various types of *nix and whatever version of windows (a few macs as well). How sophisticated you need it to be depends on the audience served.
Create a bat/shell script which starts your application.
And than create a shortcut/launcher for it.
Shortcut file in windows has extension lnk.
Update
See example - SQuirreL launch file.

java.awt.HeadlessException - Applet not displayed

I am using Tomcat 5.5.23, JDK 1.5 on HP Unix. We have an application which when invoked form tomcat starts an applet. It was working fine till JDK 1.4. But now we have moved to JDK 1.5 and the applet does not start. The exception thrown is -
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
I then added JAVA_OPTS="-Djava.awt.headless=true" to catalina.sh file. But still I get the same Headless exception, but this time without the X11 Display message.
Any help would be appreciated.
Odd.. you're trying to run an applet (I assume you are talking about a subclass of java.awt.Applet) inside tomcat? Generally this won't work because there's no display on which to display the applet.
Assuming you don't want the applet to display anywhere and you just want to execute some portion of it programmatically, you may be able to get by using a virtual X server such as Xvfb or Xvnc. Once you have Xvfb or Xvnc running on your host running tomcat, you might try to set the DISPLAY inside the tomcat startup scripts to use the display of the virtual X server.
-Djava.awt.headless=false
add above in your Tomcat startup script. it will work 100%
You are maybe using something in your Java code that can not work on a headless system, such as graphics components (Swing objects, images, etc.). Some of these components, instead of being directly handled by Java, are handled by underlying platform (Windows kernel itself or X-Window server on Unix). This way the overall performance of application is boosted.
So the question now is, ok if it was working on Java 1.4, why doesn't it work on 1.5? My bet, given the peformance boost since Java 1.2 that Swing has received over time, is that Sun has moved the management of some graphic objets to OS level to increase performance. So if you can not stick to 1.4, then you should revise your code.
This good article will help you understand how to modify your application to make it headless-friendly.
Applets are going to have a hard-time running server-side. They are designed to run inside of a container, such as a web browser. The exception is getting thrown most likely because the applet is trying to draw it's GUI -- and the server is providing no support for this. I'm surprised that it worked in JDK 1.4 -- I don't know what changed between the two revisions which would have affected this.
You may also have to install the x11 libraries, or at least explicitly export the path to them.
/usr/X11R6/lib
Open $CATALINA_HOME/bin/startup.sh file with your preferred text browser
Paste this line export CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=false" at the beginning of the file
Save and close the file
Restart Tomcat
In spring boot with database access, when you specify asterisks: **** as username and password, it will try to prompt the user for a username and password (you read that right), and it will throw this HeadlessException if it's not a gui application.

Categories