Java Console Pushes Input Text When Logging Occurs - java

Through MobaXterm's SSH feature, I'm running a Java application on a remote Linux server. A problem arises when I attempt to type into the terminal (to process user input requests via Scanner) and any logging occurs. The text I'm typing is automatically pushed into the logging section when any print statements happen.
Clarifying example:
I manually type "MY_INPUT_TO_SET_SOME_VARIABLE 50" into the console (and never press ENTER).
Some logging on the server occurs and automatically "sends" the manually typed "MY_INPUT_TO_SET_SOME_VARIABLE 50" into the display area.
(above, you can see 50 is appended to 09:08 when I never pressed enter).
The desired behavior is to allow the power user to simply type text in the terminal's text area (or somewhere reasonable) until the ENTER key is pressed. The text in the terminal's text area should not automatically be pushed upon logged or printed statements. I looked in terminal settings and wasn't able to find anything to modify this behavior.

As others already mentioned in the comment section there is not much you can do about that behaviour.
However usually you don't want logging on the tty you're working with.
If you have root rights on the system you connect to try to suppress the log messages on the console and redirect them to a logfile unless there is a good reason not to do so. Since it depends who is sending the messages the method to do so differs.
Another possibility is to start a screen session in your terminal to open a new tty.
For ease of use I would connect directly into a screen session:
ssh -t user#server /usr/bin/screen
If you create a .screenrc file in the home directory of the user you connect to, put
startup_message off
in it if you don't like the screen start message. You can even start your console app with it, so that the screen session ends when you stop your app.
ssh -t user#server /usr/bin/screen your_start_command_here
Screen has more features like naming a session, reattaching to a session etc. See the manual for further details.
(The screen solution apparently only works if the log messages on the screen are not produced by your application. In that case configure your logger that it does not log to stdout)

Related

How can I perform git push within a process?

My goal is to perform git push programatically rather then from a console window.
To this aim I create a process like:
val processbuilder=new ProcessBuilder(List("git.exe","push","origin","master").asJava)
process=processbuilder.start()
Now, the problem is that git.exe seems to start an other process, to the output/error and input of which I have no access. I know this because if I run it without internet connection, it just prints a message on the error output and quits, as it should. However if I do the same with having an internet connection, it prints nothing on its standard output/error. Also I cannot destroy the process in that case ( see : http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4770092 ), which is an indication that authentication takes place in a separate process that is created by git.exe and to which I have no access.
I tried to get around this by starting cmd.exe as a process and issue git push as a command, but also in this case writing the password to cmd.exe's standard input has no effect.
The platform I'm using is Windows 10 + Scala sbt.
What is the way out of this?
Edit:
I'm trying to do this from a JavaFX based Scala application rather than from a console Scala program.
Edit2:
Using the sys api here does not solve the problem. If you try to perform the gith push through sys api call, then two things can happen:
If there is no internet connection, it will print an error message on the gui application's console window ( it has one, if you run it with sbt run ).
It there is an internet connection then it will print nothing and you cannot perform the authentication. The process simply detaches and becomes unreachable for you.
The point is that git.exe seems to start a subprocess and you have no access to this subprocess. This is problem is special to Windows to which I gave a link in the question. When you run git in the Windows console window then it takes care of the sub processes input and output for you. You are however not in the same position as the operation system to do this.
I came up with a poor man's solution. It will run in a system console window but at least it can be started from the gui app.
The mixture that finally works is this:
1) Create a Windows batch file ( call it "temp.bat" ) which contains the command:
git push origin master
2) Start cmd.exe as a process and to its standard input write the following line:
start temp.bat
This will open a console window and initiate the git push. The authentication will be handled properly, you are prompted for the password and can type it in the console window.
Other versions don't work. For example 'start git.exe' won't open a new console window.
You can use Scala sys.process API.
import sys.process._
val statusCode = "git push origin master" ! // one ! for the statusCode
Or:
val commandOutput "git push origin master" !! // two ! for getting the output

Java: Can't connect to X11 window server using 'localhost:1.0' as the value of the DISPLAY variable

I have created a web application which makes use of JOptionPane to display some dialogs to the users such as 'Record not found etc'. I did this development in Windows and everything was working fine.
I was then asked to deploy the project war on a remote server which is a RHEL machine without any GUI (similar to AWS machines). When i run my we application over there i get the below mentioned exception wherever there is a JOptionPane statement.
Exception in thread "http-bio-8080-exec-6" java.lang.InternalError: Can't connect to X11 window server using 'localhost:1.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:142)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
at sun.swing.SwingUtilities2.isLocalDisplay(SwingUtilities2.java:1393)
at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:1563)
at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:147)
at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1599)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:530)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:570)
at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1320)
at javax.swing.UIManager.initialize(UIManager.java:1407)
at javax.swing.UIManager.maybeInitialize(UIManager.java:1395)
at javax.swing.UIManager.getDefaults(UIManager.java:644)
at javax.swing.UIManager.getString(UIManager.java:790)
at javax.swing.UIManager.getString(UIManager.java:807)
at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:610)
I have already tried to use export DISPLAY also tried
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
but still my problem is not resolved? Any inputs? Should i not be using JOptionPane in a web application? if not then what is a better option? alternate of JOption?
You absolutely cannot use a JOptionPane in a web application. And don't even think of using any other of the JWhatever classes in Swing.
JOptionPane, and various other Swing components, appear only on the machine they are running on. So, even if you could get past the exception you are encountering, and display the JOptionPane somehow, the JOptionPane would appear on the server1. It would not appear in the user's browser, so the user wouldn't see it.
So, suppose this JOptionPane has appeared on the server. How is a user supposed to read it, or dismiss it? All they have access to is their browser.
The simple answer is that they can't.
Worse still, JOptionPanes are modal, in that the call to create them doesn't return until the dialog is dismissed. If a user entered a model number that did not exist, your web application would appear to hang, as it could not finish serving the page until the JOptionPane was dismissed. With a few more users using your system, the entire application would eventually grind to a halt as more and more of the web application's worker threads get stuck waiting for JOptionPanes to be dismissed.
Instead, you need to send the error message back to the browser somehow.
If the user enters a model number that does not exist, I would recommend redirecting back to the page where the model number is entered, and showing on this page a message that makes it clear to the user that they were redirected back because they entered a nonexistent model number.
Your code may have been working for you on your machine, but that would only have been because you were browsing your web application from the same machine that the web server was running on.
1: Technically you could show this JOptionPane to a client if you had an X server on the client machine and could set it up as Jim Garrison describes, but that is most certainly not something you can assume.

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.

checking browser history from cmd or java

Is there any way I can check my browser history via java program or command line?
My requirement is I need to check how many times a person has accessed a particular site in a day from the browser.
Is there any way I can achieve this ?
You can view cached DNS entries from CMD.
Every time you browse to a web page, your PC has to request the IP Address for that web site from your default DNS server, and then caches them locally on your machine for a while to speed up future access to that web site — even in a private browsing session.
You can see these for yourself by opening up a command prompt and typing in ipconfig /displaydns to see the full list of cached DNS entries.
Now, the first thing you’ll notice is that a whole lot of information will appear, to the point that all of it can’t fit in the command prompt. What you can do is take the output of the command and send it to a text file somewhere on your machine.
ipconfig /displaydns > c:\desired location
Now, when you open that file you’ll see information about the websites you've visited directly or indirectly.

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