I'd like to use the filesystem to pass information between a java process and a native process. I would like the java process to open the file and wait until a certain word is written into the file. My specific need is for Android, but I'm guessing any answer will work of other versions of Java.
I imagine I can open the file again and again, looking for the word that the java process expects to be written into the file, and sleeping between opens. But it feels like there might be a more effective way.
I prefer to use file system because sockets (which are far more natural for Java) can be a little complicated to use from native code in my set up. I'm also okay with working out something using pipes, if it makes a difference.
One more important note: the Java process is a simple command line process, not an APK app.
What is the best way to do this?
The StdIn library is one of the easiest ways to accomplish this, here are the docs. You could run the program with:
java File < input.txt
And write something like:
String word = "x" // whatever String you're looking for
while (!StdIn.isEmpty()) {
String test = StdIn.readString();
if (test.equals(word))
// do something
}
Related
From what I read, there are a couple of ways to run java files in a node.js application. One way is to spawn a child process: (the java code is packaged with dependencies in an executable jar.)
var exec = require('child_process').exec, child;
child = exec('java -jar file.jar arg1 arg2',
function (error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null){
console.log('exec error: ' + error);
}
});
The other way is to use the java - npm module (link), a wrapper over JNI (this will let me create objects, set and get attributes, run methods).
In a production environment, when I want my node.js (Express) server to call a java program (it just saves an image to the local directory), please advise me on which would be the better way to accomplish this (in terms of best practices). Also, there is a long list of arguments that I need to pass to the main class and doing that on the command line is a bit of a struggle. Should I make the java program read from an input file instead?
1) If you use exec, you will run an entire program, whereas if you use a JNI interface, you'll be able to directly interact with the libraries and classes in the jar and do things like call a single function or create an instance of a class. However, if you don't need anything like that, I think using exec is far simpler and will also run faster. Sounds like you just want to run the Java application as a standalone process, and just log whether the application finished successfully or with errors. I'd say it's probably better to just use exec for that. Executing a child process this way is also far better for debugging, debugging JNI errors can be very difficult sometimes.
2) As for whether or not to read arguments from a file, yes, it's usually better to read from some sort of file as opposed to passing in arguments directly. It's less prone to human error (ie. typing in arguments every time), and far more configurable. If someone like a QA engineer only needs to edit a config file to swap out options, they don't need to understand your entire codebase to test it. Personally I use config files for every Java program I write.
You can use deployment toolkit and run the jar through jnlp. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html
Advantage of running jars through jnlp is the ability to pass parameters from javascript to your jar. In this way you can dynamically customize your java program.
For this kind of problem you'd want to approach it in the following way:
Is there a decent way to run processes with arguments in my language/framework
Is there a decent way to deal with the programs output?
From experience, a decent way to deal with arguments in a process is to pass them as an (string) array. This is advantageous in that you do not have to resort to unnecessary string interpolation and manipulation. It is also more readable too which is a plus in this problem setting.
A decent way to deal with output is to use a listener/event based model. This way, you respond appropriately to the events instead of having if blocks for stderr and stdout. Again, this makes things readable and let's you handle output in a more maintainable manner.
If you go a bit further into this, you will also have to solve a problem of how to inject environment variables into your target program. As an example, you might want to run the java with a debugger or with less memory in the future, so your solution would also need to cater for this.
This is just one way of solving this kind of problem. If node is your platform, then have a look at Child Process which supports all of these techniques.
We can run the whole java project by making .jar file of it and run it using the command in the shell and run that shell file. In order to run java code from nodejs project as we know project could be a mix of java, js modules.
Call exec() function in node to create a child process to execute the shell file having a command to run .sh file and can also pass some argument in it from use.eg;
let fileName = 'someFile.txt';
let userName = 'Charlie Angle';
exec(`sh run.sh --context_param
paramFilePath="./storage/${fileName}" --context_param userName="${userName}"`, (error, stdout, stderr) => {// Some code based on execution of above command})
You can simply call a java command , with classpath & arguments, using module node-java-caller, it embeds the call to spawn and will also automatically install java if not present on the system
https://github.com/nvuillam/node-java-caller
I want to know what is the easiest way to pass a String from my Java program to my Python program. The reason is that I use boilerpipe to extract some text from the web, then analyse it through my Java program, but I also have to make some semantic searchs using pattern.en, which is only available for python.
I don't need to get results back from my python program, just that my Python program can get the String.
I first thought about letting my python program listen to a .txt file, while java feeds it with the String, but I think it'll be too hard to make.
I'm on Windows 7, and my Python version is 2.7.9.
EDIT : I think I haven't been very clear actually. Ideally, I'd want my java code to run my python program which would have recieved the string.
Graciela Runolfsdottir's 2nd answer would maybe do the trick, if I can make something like that :
String s = "string to analyse"
FileUtils.writeStringToFile(new File("test.txt"), s);
Then execute it with : python analyzer.py test.txt, but I don't know how to call this command from java. Is it possible ?
The easiest way to accomplish this (and historically the "canonical" way) would be to write the string to stdout with the source program (the Java program in your case) and read from stdin with the sink program (the python program).
To link the input/output, you could use "Command Redirection", specifically the pipe:
java -jar source_program.jar | python sink_program.py
You can pass String through:
command line argument (it should not to be long and must be escaped)
file (just save string to file and pass it file name to Python application)
STDIN (output your string to STDIN of your Python program)
I think the last way is simple in implementation and you should choose it.
Different approach: try using jython. That might you to actually run python code "within" the context of your JVM.
Or if you are really concerned about having a "solid" solution based on a profound architecture; consider using a message bus; like ActiveMQ which has clients for both Java and Python.
So I've been looking around for a way to write from an Arduino directly onto a file on the PC, and basically I've found out there's no native way to do so. I wanted to do this in order to then read the file from a C++/Java program, and use the information in it. I also wanted to do this in real-time at some point, so it would be kind of like sending information from the Arduino over to the Java/C++ program for processing.
However, I've seen multiple people state on other forums that you can link the Serial output to some program running on the PC, and then use that program to write the output to a file. However, each time, they neglect to write out how exactly to do this.
The main purpose I wanted to write from the Arduino directly to a file was to read this file from another (Java/C++) program, so the above would be great for me. So how can I get the Serial output into a Java (much more preferably, as I might want to use Swing later) or C++ program, to then use this information in the program itself, or write it to a file? Real-time sending would be a great help.
If the above isn't possible, MATLAB might do, but to be clear, I would much rather be able to interface with Java/C++. Or both Java and MATLAB.
EDIT: To be more specific about what exactly I'd like to do, it is to sort of 'trigger' the Java program to read from the Serial output when a new line has been written (so it reads each line separately) and store it in a string in the Java program, then process it, all at once, and then sleep until another new line is written to the Serial port.
The following Links show you, how you can implement the serial communication in Java and C++.
Java :
http://playground.arduino.cc/interfacing/java
C++ :
http://playground.arduino.cc/Interfacing/CPPWindows
If you want to write the data stream to a local file, you can do that, for instance, with ofstream (C++) or PrintWriter (Java).
Furthermore, there are a several additional libraries for other programming languages such as c# (cmdMessenger).
Just in case anyone was looking for the easiest way to do this, it is hands down to ignore C++ and Java and use MATLAB.
There's a great short tutorial at AllAboutEE that I used and it solved most of my problems. Instead of plotting the data at the end, just use fprintf in MATLAB to output the data to a file.
I am currently trying to have Logstash work on Solaris with the File Input method. But I am encountering some bugs (see LOGSTASH-665). After digging a lot, it appears that native support for File.stat on my system (SunOS 5.10, JDK 1.6.0_21, 32 bit) is totally deficient, so I am looking for a way to properly handle it.
Specifically I want to access the inode information. Based on the metadata I can gather about the file (like its path and whatever is available on solaris), I want to calculate a number which is unique for that file, and which changes when the file is replaced by another file which has the same name. At first I thought about simply using a hash of the file path and used this function as a replacement, but indeed, when the file is rolled over the number does not change, so I need to also access the ctime information...
..Or make a system call to get the ls -li result to get the real inode number by another way.
Problem is that I never used ruby before, I am more used to java, so I am struggling to find a solution. Every suggestion will be appreciated.
The best solution I know of is to wrap the native call using JNI or JNA.
There do appear to be a couple of projects that have done this, although I haven't used either of them. See this question: Is there a Java library of Unix functions?
I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:
=====================
You just need to make sure that stdin for kgsGtp it connected to
the engine's output and stdout for kgsGtp is connected to the engine's
input. Usually, the easiest way to do this is by forking and execing
kgsGtp from within your engine.
=====================
Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.
I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?
That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.
Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.
Edit: more complete example is here: http://support.microsoft.com/kb/190351
What you need is equivalent of POSIX dup() on windows may be this