Bypassing event watcher in java program with batch file - java

I'm currently trying to automate some data export from a monitoring program written in JAVA. I don't have the source code so I got a JAVA decompiler to look inside my .jar file.
I found the functions that I want to execute but I don't manage to execute them by commands in cmd prompt.
When I launch the .jar file, the main() function starts, it launches two event watcher threads that wait for actions. The actions that I want to perform are normally accessed trough buttons pressing.
I tried launching hese functions directly but my batch lines aren't even read. The only read line is the first one, launching the program, then nothing, it just waits and doesn't read the other lines.
I'm a bit lost and I could sure use some help !
Thanks in advance

Related

Running server-program from a .jar file

I have created and am working on a server-application that monitors for specific folders and takes appropriate actions whenever files are being added.
Now I come to the point where I want to be able to shutdown the program, for example for applying a patch.
The server runs simply in a command prompt, how can I signal that I want to perform maintenance on it? I do not think reading System.in is feasible as I am also outputting text in the prompt.
Regards.
You could try reading System.in as System.in and System.out are different file descriptors. What this means is that by writing things in console you are not writing in the same place than when you are typing, so console output should not matter for reading commands in the prompt.
A second application can be used to communicate with the server application. You can use Java Management Extensions or implement your own client/server communication using sockets.
Another way to achieve this is that server periodically checks for existence of an specific file somewhere on hard disk. If server finds that specific file, it will shut down.

Can I use a servlet using "Runtime.getRuntime().exec(Cmdline)" to run another program (exe file) and write to and read from text files?

I have an eclipse project (let's call it myapp.java) that is a java SWT GUI with a few text boxes and push buttons. When the user enters the required information, it writes a text file and creates a string called command-line . It then uses the run-time command:
Process process = Runtime.getRuntime().exec(Cmdline);
to execute another program, written in C (lets call this myapp.exe). The command line contains myapp with various command line options. This program reads the inputfile.txt and writes and outputfile.txt.
The java program then copies the file outputfile.txt into a window.
Can I make this application into a dynamic web application and place these two programs on a web-site and execute them on the host machine? If so, what are the steps involved? I will need to run an exe file on the host machine from a java program and read and write text files on the same machine.
The cmd executing part will be the same as for SWT. It should be no problem to reuse your existing code, if the SWT GUI and the cmd execution logic are are nicely separated. Of course the GUI (web) part is completely different.

An application to get Userinput and to run commands background in windows

I want to create one standalone executable file which accepts some user input and it should use those inputs as arguments to run a command at CMD as background process. For GUI i thought of using swings and to run cmd batch file . Is it possible to merge java files and batch file to form an exe.? or i should go for someother scripting languages to execute command at background process and someother programming languages for UI design ? Thanks in advance.
Theoretically you could do this all in Java. As you mentioned, you could use Swing to read in the user input. Instead of trying to get ahold of the input data from a separate batch file, you could just use Java's Runtime class to execute a command using the inputs as arguments.
And, if you need this all to run from an executable, you could use a utility like Launch4J or JSmooth to wrap the Java application.

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 .

Java monitor file system when java is not running

I recently implemented Java 7's WatchService and it works perfectly. Now I wondered if there is a way to get all the Events which occured since the last run of my program. For example:
I run my program, create some files, edit some files and I get all the corresponding Events
I close my program
I create a file named foo.txt
I start my program, and the first event i get is an ENTRY_CREATE for foo.txt
I thought about saving the lastModifiedDate and searching for files and directorys newer than the last execution of my program. Is there another (and better) way to do this?
There is no better way to do this if your program is meant to scan for all file changes (apart from storing files in a content / source control repository, but that would be external to your program).
Java 7's WatchService is only a more performant way than continuously looping and comparing file dates / folder contents, hence you need to implement your own logic to solve your own problem.
There is no way to do this in Java, or in any other programming language.
The operating system doesn't (and can't) buffer file system events on the off-chance that someone might start a program to process them. The event monitor / delivery system captures the events for a running application that is listening for them. When nothing is listening, the events are not captured.
You could write a small daemon (system service on Windows) which runs continuously and listens for file system changes. It could write these to a file. When your application runs, rather than listening for changes itself, it could just read the file. As events happen while it runs, the daemon will continue to receive them and send them through the file to the application.
You would need to ensure that the file was organised in such a way that it could be written to and read from safely at the same time, and that it did not grow indefinitely.

Categories

Resources