How to run shell script from Java based code - java

Basically what I need is, I want to run a shell script present on remote linux machine along with that I need to pass one list collection as an argument to that script and fetch the result back in java code to print and store in object.

I think Old post in stackoverflow will help you on this
How to run Unix shell script from Java code?
Run shell script from Java Synchronously

Related

Using 'become' command in Java JSch program to access a restricted directory

Working with Unix server... My requirement is to read the name of the file that is there at /a/b/c/node01/d.ear location on a Unix server and I have do the same through a java program. The problem is that the directory a is a restricted directory and is accessible only to certain users. On the Unix side, I first issue a become command like become a, then supply the password and then using cd command, I reach the d.ear directory and then get to see the name of the file.
How do I do all of this via a Java program?
I don't mind if my Java program calls a shell script that accesses the restricted directory and then reach d.ear and fetch the name of the file and returns the same to the java program. Do we have a way of doing this? Maybe issuing the become command inside the script which is called from the Java program and the password which is asked after become command is supplied as a parameter while calling the script???
Is this approach doable? I am very new to Unix commands and JSch library. Kindly provide the code or any other alternate solutions...
Thanks!!!
As I have suggested you already, your become command seems to behave the same way (from an interface/API point of view) as common *nix su or sudo.
So, use the same solution as for those. There are many questions on Stack Overflow covering use of su/sudo with JSch.
There's even an official JSch example Sudo.java:
http://www.jcraft.com/jsch/examples/Sudo.java.html
In short:
Execute become command
Feed a password to its input
Assuming the become starts a new shell (as su or sudo do), you feed the commands to be executed in the elevated environment to become input (the same was as the password).

How to call a shell script from UI with out App server and web server?

I have a shell script and argument as a date. I'm looking for some help in design, where I will have to create a UI and pass the date, so that it will call the shell script with an argument (it should open unix terminal and call the shell script with username and password). The whole shell script runs on LINUX server. I would preferably be on MarkLogic/Java/Unix and Scala. I should not use web server in my application.
Can someone please suggest how to call the shell script from Java application with out using an Appserver/Webserver.
I find your actual question quite confusing and even more so after your clarification to nhouser9.
What I interpret is :
1) You need a user interface (but not a terminal?)
- Your simple need may be addressed with the tried and true old timer Java Abstract Windows Toolkit (AWT). Have a look at the wikipedia page: https://en.wikipedia.org/wiki/Abstract_Window_Toolkit
The sample code there compiles into a very simple Hello World GUI. (Java package info here: https://docs.oracle.com/javase/8/docs/api/java/awt/package-summary.html)
2) You need to call a shell script from Java:
Then for this part of the question, have a look at nhouser9's original comment. That answer can be found here: calling-shell-script-from-java The heart of this is ProcessBuilder
Add 1 and 2 together and you can create a GUI to take a parameter and then execute a shell script.

Pdf Compression using java

i developed application which will converts tiff,jpeg,pdfs in a zip file to single pdf,and its working fine and size is also similar to original files , but i need to compress the pdf ( save my file server memory) i searched in google , i got ghost script which will reduce the size of 5mb pdf to 50kb,and i manually executed same in linux and its working fine,but i want to run same command in java programme,
but i dont know how to run linux command in java programme since i am new to java i am facing difficulties,can any one post sample programme how to run linux commands in java ,so i can try myself
What you want to do should be able to be achieved by simply running a bash script, which is pretty simple. Also it happens that someone has asked that question before so that answer should in turn answer yours. Please let me know if you want a more detailed response or need help with the implementation.
Running a bash shell script in java

Java/python using processBuilder

Good evening all,
am running a python script inside java using processBuilder.
the python script returns a list and i dont know how to get it java and use it since all i can do for the moment with process builder is print errors or outputs.
is it possible to get the list in java as well.
Many thanks
From your scenario, you are looking for inter process communication.
You can achieve this using shared file. Your python script will write the output in text file, and your java program will read the same file.

Trouble in calling system commands from Java

i have this problem:
I have created a bash script that performs some tasks. At one point this script call a java program. This java program perform some wsdl tasks and in one point have to call an external fortran program that perform a simulation and put the outcomes inside a file "oucomes.dat". program.exe is perfectly executed but the java program seems unable to open the file created by the fortran program. The code in the java program that call the fortran simulation is:
Runtime.getRuntime().exec("./script.sh");
where script.sh contains
#!/bin/bash
./program.exe
When i call first program.exe and then the java program the java program can read prefectly "outcomes.dat". The problem is that i have to call program.exe from inside of java because i need some data in realtime from a wsdl service and eventually send data back to the wsdl service. So i guess that the problem is in the form that i call program.exe from inside the java. One solution may be to split the code in java in two program and putting between the to program a call to program.exe. But i would like a faster solution (in terms of CPU and memory usage). Which is the correct form to call the program.exe in order to allow the java program to read the "outcome.dat"?
PS: i use linux.
It doesn't look as though you are waiting for script.sh to finish. You need to do something like this (untested):
Process p = Runtime.getRuntime().exec("./script.sh");
p.waitFor();
You can also test the process's exit value using exitValue().
See http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html .

Categories

Resources