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.
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'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
}
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'm writing a text/code editing program for my own use in Java/Swing, and was wondering how I would go about setting up a built-in C compiler inside it. I would likely use GCC or TCC as the compiler. Anyway, my question is- how would I actually implement the compiler? Would I use a library that gives Java access to command line commands? Are there such libraries and if so, which is the best/easiest to use?
Thanks.
Accessing command line is the easiest way.
Try something like this:
Process myProc = Runtime.getRuntime().exec(command);
Where command is some string you want to pass to the command line.
After that you can redirect output / input of that process to the some java buffers to have the full control.
myProc.getInputStream();
myProc.getOutputStream();
Typically IDE/Editor's don't implement the compilers. They will just execute the commands and pass the filename as argument (along with other necessary files). They also pipe/stream the output to a separate window/pane in the editor. So you need to integrate the compiler somehow not implement one. You can execute the commands in java using Runtime class. Start here.
Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...
Basically I have installed Cygwin with an SSH client and want to do the following:
Open Cygwin,
Send command "user#ip";
Return output;
Send command "password";
Return output;
Send multiple other commands,
Return output;
...etc...
So far:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
Works nicely except I am at a loss as to how to attempt the next steps.
Any help?
The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.
A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.
You could also use Plink:
Download here
There is a good set of instructions link here
You can use a command like:
plink root#myserver -pw passw /etc/backups/do-backup.sh
Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)
Using Ganymede, you will get an input stream to read from, and an output stream to write to.
You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.
Create a PrintWriter on the output stream and use println() to send your commands.
Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)