How can I perform git push within a process? - java

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

Related

Windows could not start service, Error 1053: The service did not respond to the start or control request in a timely fashion

I have created a Java program then I need to install it as windows service.
First I run the jar directly from console and it ran as I wish, then I compile the jar to exe and I ran it as Admin ran OK.
But when I run it from windows service I got following error,
Error 1053: The service did not respond to the start or control request in a timely fashion
I also tried to set Account and password on Log On Tab, but not resolved yet.
It's not possible to resolve the actual problem with the information given by you, so I will provide you the steps necessary to debug the problem.
Use logging in your application to see what causes the error: Apache Log4j
You can use the Windows Event Viewer to see what happens when you start your Windows Service. This is very useful if the problem happens before the start of the application itself.
Click on Start / Windows symbol, type Event Viewer and press Enter. In the left hand tree-menu, click Windows Logs and then Application.

How to execute MSDOS Command from browser (PHP or Java)

I have a USB line display, similar to the ones that are used in store point of sale systems.
To print ABC to this line display, I run the following command:
echo ABC > \\.\LCLD9\
This prints ABC to the line display.
I'm trying to figure out how to control this output from a website, if possible, although I am not sure if it would be possible, as I can see security issues with being able to run arbitrary commands form web pages.
I'm trying to figure out how to control this output from a website, if possible, although I am not sure if it would be possible, as I can see security issues with being able to run arbitrary commands form web pages.
This is indeed not possible on an unmodified system. There is no way for a web page to run an arbitrary command on the user's machine -- that is the definition of a security vulnerability.
If you can get the user to install software ahead of time, you could use a Chrome extension with the serial, usb
or nativeMessaging APIs to expose this functionality to web pages.
Check out function shell_exec. http://php.net/manual/en/function.shell-exec.php
There are several ways to do this. One way is to simply have your website store the current text somewhere and then build a script on the local PC that asks your website for the current text and updates the display. You can schedule that script to run periodically using the Task Scheduler.
This script can be as simple as this (assuming PHP is installed on your local PC):
<?php
$text = file_get_contents('https://example.com/current_text.php');
shell_exec('echo "' . $text . '" > \\.\LCLD9\');
If you don't have PHP installed and don't want to install it, you can probably do something similar in a Powershell script or in a plain old batch file.
Note that you probably want to add some extra checks to this, you don't want to accidentally clutter your display with a 404 error page, a 500 internal server error or a network error.

Java Console Pushes Input Text When Logging Occurs

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)

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.

Backend generates pdf files (labels, packing slips, etc) - Need to automatic print to pre-selected printers from browser - Workaround?

I'm in the process of developing a web application; where in the admin backend I need to have the functionality to do automatic print jobs for different processes that run and generate a pdf (labels, packing slips) in admin that will be sent to pre-selected LAN network printers in my warehouse.
I've been doing some research on this and I know due to security issues automatic printing like this is hard to configure. However, I have some posts where people write that they were able to pull this off with active x, java, print server, client software, etc...but there isn't a clear outline on how exactly to do this.
Can someone help me figure out a workaround that I can use to be able to automatically print to different LAN network printers in my warehouse when a certain process runs and generates a pdf file?
Thank you!
I also manage a warehouse system that does these tasks. I will not claim that the following solutions are the best way to go, but they have been working for us. Our system is built using PHP 5.3 on a Windows server using Apache. With this setup, the user does not need to print anything from the browser, it's all handle server-side.
Requirements: Apache needs to be running with Administrator privileges. Probably not recommended for web-facing servers.
To print a PDF on Windows via PHP:
$file = "c:\\path\\to\\file.pdf";
$exec = '"C:\\Program Files\\bioPDF\\Acrobat Wrapper\\acrowrap.exe" /t ';
session_write_close(); // prevents hanging
pclose(popen($exec. $file . " \\networked\\printer",'r'));
This just launches reader, prints the file, and closes reader.
On a Linux/Mac you should be able to use (without Admin privliages):
$file = "/path/to/file.pdf";
$command = "lpr -P /printer/path " . $file;
exec($command);

Categories