Here's what I'm looking at doing. I've certain log files on a unix box. The files are in the range of 10-100 MB. What I'm looking at building is have a web application in which I can have the unix command line, where I can type my commands like grep,ls etc and then the output that the command gives can be displayed in a table/gridview in the web app. Is this possible to do? I'm looking at a solution possibly in Java. The only thing I cannot use is Microsoft stack.
Have you tried the web SSH clients listed here: http://en.wikipedia.org/wiki/Web-based_SSH
java Runtime can help you. It will allow you to create process and execute the command inside the process and get the results back.
Related
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).
Mac OS has an application called Console, which contains logged messages, errors, and faults. I believe the Windows equivalent is Event Viewer. I'd imagine there is one on Linux as well, but I don't know anything about it or where it is.
Is it possible to get a message from Java output to a system log like this? I'm writing a GUI-based application, so there is nothing running from the command line. The standard System.out or System.err probably won't be of much use in this case, unless I'm missing something.
I have written a simple logging service for my application that writes to a dedicated log file, but I want to have some kind of failsafe in case an I/O error occurs while attempting to write to this file.
I know the IDE will display output via System.out and System.err just fine, but this is for if the end-user encounters a problem like this.
As an example: I've written "codeless language modules" for the application TextWrangler on the Mac. These modules are read by TW at application startup, and if there is an error while processing them, errors get logged and can be viewed in the Mac Console application.
On Linux it is called as syslog. One of the ways that you can achieve logging to console on Mac will be to use log4j 'org.apache.log4j.net.SyslogAppender'.
I think this link should give you some kickstart in this direction.
You can redirect System.out to a log file, and Mac's Console app is the default viewer for files ending in ".log".
One way this is commonly done is with a shell script that would invoke your Java program. In this Java 7 example, the output of invoking the main class MyClass is redirected to mylogfile.log. Everything that's written with System.out will be in mylogfile.log.
#!/bin/bash
for a in /path/to/my/jars/*
do
CLASSPATH=$CLASSPATH:$a
done
java -Xms128M -Xmx128M -XX:MaxPermSize=128M -cp ${CLASSPATH} com.example.package.MyClass >> mylogfile.log
I have some mex files that urgently need to be called via MATLAB, there is currently no way around. However, I really despise MATLAB's GUI (in)possibilities and would like to create some e.g. JavaFX Apps.
My question: how can a Java app's communicate with a running MATLAB instance?
I know that you can include Java objects into MATLAB, however I would prefere to have a standalone Java app.
Java can execute commands via command line for example:
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
So it is possible to execute a MATLAB script via command line in Java.
In MATLAB it is possible to write files with any data needed. I don't remember the exact way you may do this. http://www.mathworks.com/help/matlab/ref/fprintf.html gives an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
It is some kind of a workaround but it should work and it is not really hard to implement.
Update.
If Matlab is already running and you want to communicate with it in another application (Java), it may be done using a network connection through the localhost. Matlab may listen to some predefined port (for code example see http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example ) and do some action when a "start" trigger is sent via Java (or even some data along with the trigger). In Java you may use the Socket class (some code example may be found here http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html ).
Also it may be done writing data into files. For example, Java adds some command to some file with predefined name (command.txt). Matlab scans this file in a loop and when something is found there it starts calculation (and Java application waits for results in some results.txt file).
I would suggest to start a server in Matlab that listens on a specific port to send/receive data to/from a Java client. By using the eval Matlab command you could even invoke scripts/command/etc. remotely controlled by a Java client.
You might want to have a look at this code example.
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
I have a CentOS server which is currently running a java jar application. I need to write a php script to communicate with this running program via its input stream. The java program outputs its output to a log file so I don't need access to the output stream.
I don't want to restart the program, just access the running process and interact with it.
Can someone point me in the right direction?
If portability is not a big matter for you, why not creating your own pipe(s)? I don't know much about the java application but have a look at the "mkfifo" function/command.
First, find the ProcessID of the application. You may do it using:
ps -Af | grep java
Since you are using java, you may feel more convenient with the jps command for finding the PID.
I'll assume PID of the running application is 12345. It suffices to issue the command:
cat >/proc/12345/fd/0
And whatever you type in will be put in the standard input of that application. Note that fd contains the file descriptors used by the application, and I suppose the 0-th file descriptor would always be stdin.
Using PHP for writing into the file (and thus being consumed by the application as input) is possible as well.