Running Java UI program though unix - java

I want to facilate my client to run java program through UNIX command prompt using some shells. It'll look more effecient if they would be able to give input through some GUI. So it can be tested immedietely. I dont want prefer unix commands fro input.
Can somebody tell me how to run Java swing or applet programs in UNIX?

As Thompson mentioned, looking at Java Web Start could be a good idea.
Otherwise, if what you want is to execute, using a *NIX-like terminal, an application located on a remote host and have it rendered on your local display, then you need to do a few things:
you need a working X server on the local machine
you need to export the DISPLAY to the local machine (you can do this by setting up the DISPLAY environment variable on the remote system)
then you need to start your Java app from the command-line.
Hope this helps.
Here's an example of how to export your display over SSH.

Java programs use the X windows system (just like any other GUI on Unix). Assuming your X windows system is setup correctly, you should just open up a JFrame and do your GUI coding just like Windows.

Using the command prompt to launch a GUI is so last millennium. If you can distribute from a server, look into Java Web Start to provide the end-user with a simple and painless install.
Oh, and of course, follow Starkey's advice to throw a JFrame into the mix.

If you have an X-server installed locally, Putty can tunnel the X11-graphics generated by Linux Java back from the server to your local machine, and view it there.
If the above doesn't make sense to you, your next best bet is either running the Java code locally with Java Web Start (and code it to communicate back to the remote server) or run Servlets inside a Java Web Server running on the remote host.
In other words, GUI over a Putty connection is not something which is easily done.

Related

Is it possible to automatically upload files when created to a web server?

I am working on a project using a Raspberry Pi and a web cam to detect motion.
I have got it to a stage whereby it takes an image and saves it on my computer. What I am wondering is, is it possible to let FileZilla automatically upload the image to my webserver when a new image is taken? Or is there any other ways that I could achieve this?
Since the post is tagged java, I'm assuming that you're using a Java program already or have the basic knowledge to create a Java program.
On to the answer: yes, you basically have two options.
1. Upload from within the Java program. FTP is probably the easiest since most web servers will have an FTP server running. Here is a tutorial you can use: http://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example
2. Use another utility outside your Java program to upload the file to your webserver. rsync would be the tool of my choice (tutorial here). When on a Linux machine (for example, the Pi) or a Mac, you can run a script that syncs the content of a local folder to a remote folder every x seconds:
while true; do <rsync command hier> sleep 5s; done
Note that that sleep period shouldn't be too short or you'll end up running multiple instances of rsync.
When on a Windows machine, you need to find another way to run a periodic process to trigger the rsync.

Ncurses not supporting color

I am writing an android SSH Client. I have a terminal object that controls the view and an SSH object to send commands to the server.
My problem is that the terminal displays in color during all sessions but when an ncurses application opens, (tmux for example), the terminal displays in black and white.
I was able to find this: http://invisible-island.net/ncurses/ncurses.faq.html#white_black
I am not really sure what that means. Can anyone guide me on more documentation on this, or if there are any open source Java clients that support this feature. I am not really sure how to fix this.
Ok, so you launched the application without the TERM environmental variable set appropriately, and that meant that during the initialization routines the remote operating system believed it wasn't talking to a terminal that could support colors.
Now that you have it set correctly, your colors work. Congratulations! However, setting it inside the application is going to be quite a trick. This is because it needs to be set before you application launches. Otherwise, when the application launches, the low level libraries you are linking into will query the "environment" to see what kind of terminal it has, which determines what kind of terminal codes will be emitted by your application.
This all happens before you app launches; so, effectively, you can't really do it from your application. However, the real solution is a bit more interesting.
SSH makes very few assumptions about the display capabilities of the remote machine. The best way to "fix" this is to have the SSH client set the terminal type according to the SSH client's capabilities. Check your ssh client configuration to see if you can pass in a "better" terminal type.
In fact, having the host operating system assume the client's capabilities will create issues; however, demanding that every ssh client be configured to hand off it's terminal capabilities properly can be logistically impossible. So, you may want to strike a compromise. On the ssh server machine, try dropping a "wrapper script" to launch the application with a color terminal script. It would read something like
#!/bin/sh
TERM=xterm-256color
export TERM
exec launch-app "$#"
and be saved as launch-app-color or something similar.

Running a Java Program on a Network Event

Well what I am trying to do, in the long run, is to change some LAN properties when the an ethernet cable is connected to a computer.
I want to run my Java program each time a LAN network is detected. I found a couple questions as to how to do this in C++, but nothing related to Java, specifically. Would this just involve the way I distribute my final application? As in, I could use Jar2Exe Wizard to package my Java program as a Windows service and then just figure out how to run that at startup. But is there any way to do this within the Java program itself?
Don't know what your specific need is but you could try this. This shows how to run windows commands from inside java so you wont have to create an external batch file.
How to Execute Windows Commands Using Java - Change Network Settings
Also check out this answer which talks about retrieving network name in java. then you can combine both!
How to get the wifi network interface name in java

How to run multiple simultaneous bash shells without a GUI?

Is it possible to have several command prompts running simultaneously and switch between them, without using a GUI?
Background
I have installed CentOS-5.5-i386 without any extras, so I have a bash command prompt with root access but no GUI as far as I know.
I have written a simple Java servlet using Jetty. When I run it, it gets to a couple of commands like this;
server.start();
server.join();
where it waits for incoming requests forever - ie. it never returns to the command prompt.
I want to run a web server without the overhead of a GUI. How can I run my Java program and also continue to use the server from a command prompt?
I apologise for the waffly nature of this question but I am both a Linux newbie and a Java newbie.
Regards,
Nigel
In the general case you want screen or tmux. For running daemons, though, take a look at nohup my-daemon & or even just my-daemon &.
You can switch between consoles using Alt+F1 to Alt+F6. For more shortcuts take a look here: http://linux.about.com/od/linux101/l/blnewbie5_1.htm

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