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
Related
I am trying to autograde some work submitted by others. Say an executable is called foo. It is supposed to write to standard out but in some cases, annoyingly, it instead opens a file and writes to that instead.
Is there some way (in linux) of running foo so that all its output, no matter if written to a file or to standard out, is piped to standard out?
I do have the source code for the submissions as well which are in Java.
(Any suggested tags most welcome. I wasn't sure what to choose.)
Added notes
I can't just give 0 to everyone who writes to the wrong place. I can of course give them a bad grade but that doesn't help me.
I don't know where they have written to. Some seem to write to files in subdirectories.
This answer shows how to wrap the write() system call with code of your own. You could do the same for open() (and fopen() and a few more) to trap writes to files. (Do click through to the earlier question it references, which has a good discussion as well as pointers to more information about this technique.) I suppose something similar is possible on Windows, although their terminology is bound to be slightly different. (The U*x libc buzzword is LD_PRELOAD.)
Similarly, if your submissions are in source form, you could hack your compiler to wrap or replace the corresponding Java primitives. If your students are entry-level, there are probably just a few common functions you need to identify and replace. But then, maybe you could just grep for them in the submissions, and be done. (On the other hand, beginners are more likely to stumble over a web page which somehow convinces them that they need to write their own operating system in order to write to standard output...)
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 want to be able to input java commands/code to execute during run-time (during the execution of a "persistent" program) in the same way, or as close as possible, to the same code would be executed if it was present on the source-code for the program (programmed as part of the software), using a GUI element, like jTextArea.
The following StackOverflow questions seem to be related, but, along with they'r answers, don't seem to be what i'm looking for.
How To Get Input From Console Class In Java?
Using GUI for console input and outputin java
I don't want to "be able to trigger actions from specific console inputs, and print the feedback into the console", as seems to be the case of console applications described in those question and answers.
What i want is to "be able to execute actual java code, that i cannot predict nor control, from console (or GUI) input".
NOTE: The "i cannot predict nor control" here refers to the "developer"...But of course i can predict and "control" it if i'm the one inputting the code, as the "user" would do.
Since java uses a virtual-machine environment, i think it might be possible for it to execute "run-time inputted code"...But i'm not sure such thing is even possible.
For example, i'd like the run-time input of for(int i=0; i<3; i++){System.out.println(i);} in the "GUI console" (jTextArea, for example) to, upon pressing the enter key or clicking a "Send" button, be ("compiled and "?) executed, and give the same output as if it was part of the original source-code, thus:
0
1
2
So i'd like to know the following:
Is it possible?
If yes, how can i do it? (OR, if no, what is the closest alternative?)
Use the JavaCompiler. It can compile code from a String.
For an E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
But note the:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
You can use JavaCompiler, as this question's answer states:
https://stackoverflow.com/a/935316/420001
Also, what you're wanting to do is evaluate a String of code:
It's not really recommended though.
There is a project called BeanShell
"In short, BeanShell is dynamically interpreted Java, plus a scripting language and flexible environment all rolled into one clean package. "
I know this is an old answer but for future Googlers:
I would recommend JavaREPL whose source is available here:
https://github.com/albertlatacz/java-repl
What AlmightyR is asking for is called Read-Eval-Print-Loop (REPL) for the Java language which is what JavaREPL provides.
JavaREPL has an online demo available here: http://www.javarepl.com/console.html
JavaREPL also has an Intellij plugin and a CLI version which are both linked to in the Github repository.
It looks sort of abandoned currently but perhaps it just doesn't need to be maintained?
I realize this may be a duplicate question, such as, Why Use java.io.Console?. However, I wanted a more detailed answer (pros/cons, applications, anything more).
Forgive me if I am misunderstanding this. So I ran a program using eclipse and I received the error message "No Console", so I just let it go and ran it via Terminal instead. However, I am a little unclear why is the console object doing this (I think it's because it returns null, but I wanted a more detailed answer). From various reads, it seems various IDEs such as Netbeans and Eclipse haven't "implemented" this properly. So it leads me to the question, and since this is my very first time seeing java.io.Console, why would I want to use this over standard streams? Examples for applications using console is appreciated and preferred!
Programs written with interactive shells in mind provide a lot of conveniences to command line users
tab completion
history tracking
password input secure against shoulder surfers
colored and formatted output
using the preferred pager for paging text
Console allows java programs that are often invoked via the command line to present a good user-experience to users who invoke it in an interactive shell.
For example, Console.readLine might sound like something very similar to BufferedReader.readLine, but when the program is run from an interactive bash shell, it behaves like UNIX readline
DESCRIPTION
readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is NULL or the empty string, no prompt is issued. The line returned is allocated with malloc(3); the caller must free it when finished. The line returned has the final newline removed, so only the text of the line remains.
readline offers editing capabilities while the user is entering the line. By default, the line editing commands are similar to those of emacs. A vi-style line editing interface is also available.
...
SEARCHING
Readline provides commands for searching through the command history for lines containing a specified string. There are two search modes: incremental and non-incremental.
why would I want to use this over standard streams?
If your program produces textual output and its users are likely to include sysadmins who will often invoke it inside an interactive shell, they might benefit from Console.
I am writing a console application in Java. It is similar to a chat client: Input and output are asynchronously made. The problem is that if some output is made while the user is in the middle of typing, the lines will get mixed up on the screen.
I am looking for a solution which allows me to have a input area separate from the output area. At the moment I am using an extra thread which polls a BufferedReader on System.in.
The program needs to run on a Linux server and be accessed via an ssh session. So any hints that only work in this environment are fine.
Are there any high level libraries which can do this? Or is there a smart trick using terminal / ANSI codes? The ANSI codes s (save cursor) and r (restore cursor) might be helpful but how do i know where to jump to do the output and how do i handle scrolling?
I recall a long time ago working with similar things but in C++. I was using the ncurses library then. Check out javacurses which seems to be a Java implementation of something like ncurses.
Sounds like you need to use Curses. JCurses is a Java implementation of the Curses library and will give you control of the terminal to allow scrolling, positioning etc.