Here is a piece of code to read user input using Scanner.
Scanner inner = new Scanner(System.in);
Logger logger = Logger.getLogger("Test");
logger.info("Before");
int a = inner.nextInt();
logger.info("After");
......
When I use ant to run a java task and excute my code(with "fork=true"), the program get stuck after printing "Before". I can input anything but the "After" never get printed.
However, when using command line java:
java -cp build/BoxBugRunner.jar:lib/gridworld.jar com.perqin.boxbugrunner.BoxBugRunner
the input is accepted and everything works fine.
It seems that System input cannot be access when using ant to run java program, so how to solve this problem?
Instead of using System.in you should:
Split your program in the part before and after the input. In the build script execute the first part, then get the input via the Ant input task, and finally execute the second part with the entered parameters.
Or write your your program as Ant task that delegates to the Ant input task for prompting for input.
Related
I'm using a compiled Java file which takes a filename as a parameter and then asks for a username and password (via user input). I am trying to automate this from a shell script, but am running into problems. I'm unable to access the java code, which is why I am having trouble.
So the code is called the following way
java javaprogram /home/user/securityfile
So, you pass the file, and then it asks you for username and password. After entering those, it's done.
Now, I have tried to put the user input into a file and pass it in, but I get an error.
java javaprogram /home/user/securityfile < userinputfile
userinput file contains the following text (First line is the username, second line is the password):
MattSmith
MSpassword136
Does anyone have any ideas? Maybe I am doing it wrong?
Here is the error message:
Exception in thread "main" java.lang.NullPointerException at ilex.util.UserAuthFile.main(UserAuthFile.java:297)
Thanks
Try
(echo "user"; sleep 1; echo "password") | java javaprogram /home/user/securityfile
If that works you can refactor to read from file without too much work. If it doesn't, may be that app will never read from System.in since it requires an interactive console System.console(). Some info here.
I have a netbeans project with test classes junit-4.1 and want to read in some numbers from console using the scanner class. System.out.println... works fine but, when I input something in the netbeans console, then code execution stops where the scanner reads in. Waiting forever...
here is the code
private int controllerByteToWhiteCell() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please choose one of the following options 1,2,3,4,5,10,11,12,13,14,15");
String cmd = scanner.nextLine();
System.out.println("cmd = " + cmd);
return Integer.parseInt(cmd);
}
running Test/File on the test file which calls the upper code lines, I get output in the netbeans IDE 7.4 console
T E S T S
------------------------------------------
Running deltaanalytics.ftir.hardware.jueke.controller.JuekeCommandServiceTest
getBytesArrayToWhiteCell
Please choose one of the following options 1,2,3,4,5,10,11,12,13,14,15
11
after entering a number 11 + carriage return, execution of code stops, no otuput from next System.out.println appears
if I run this code outside netbeans IDE it works! (without use of junit tests). Do I have to redirect System.in somehow? How can I check that the IDE console waits for System.in input?
I do have some serious troubles understanding the console in java. I am running Eclipse, and I wanted to write a small program which prompts a few text messages to the console and receives a few strings as input arguments from it. Problem is: When I run my program, it opens the command line window properly, but my outputs are only printed on the Eclipse-Console.
In some way, I do understand why this is the case. The Command Line Windows expects commands, and not just some kind of a string or something. But how do i manage to output my Strings into the Command Line Window and read Strings from it, and not just commands.
Or am I doing it the wrong way? Do I have to open another "Console" where all my messages will be prompted and from which i can read strings a user wrote?
This is the code i use to open a command line window on start:
public static void main(String args[]) throws Exception {
Process process = new ProcessBuilder(new String[] { "cmd", "/C",
"start", "cmd" }).start();
System.out.println(process.waitFor());
Edit: I did still not manage to get this to working. Somehow, when I compiled the program, and I run it, it properly opens a command window, but no messages are posted there. Seems like "System.out.println("xxx") does not have any effect on this window.
There's no "console" specified by your program, but an stdin, stdout and stderr for input, output and error output. When you run your program from windows, these streams are bound to a command window, and if you run it in eclipse, they will be associated to the eclipse console. To give a more obscure example, ff you were running it through ssh, the streams would be associated to ssh, and ssh associated to your command window, and so on.
So, you're not doing anything wrong, you just need to run the program from the command line if you want stdout and stdin to come from that command window.
How do you open a command window, by the way?
You might want to read through this page:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/JavaIO_Scanner.html
Basically what you need to is create the input stream, tell the user to input something, and then get the input. E.g.
private static Scanner newScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Put your input here: ");
String inputValue = newScanner.nextLine();
System.out.println(inputValue);
}
Just remember to import the scanner library!
I'm testing tidesdk.
I have a java program that reads from standard input.
I run the program through the console console
java -cp MyProgram.jar package.MyMainClass
And then execute commands and get results.
there any way to do with tidesdk?
Edit:
The problem was that calls the java program with a list of one element (which contained the command separated by spaces)
It solved with passing every word to a item of list (and removing the spaces).
Right now I have porblemas to write standard input. This is what I'm trying.
var input = Ti.Process.createPipe();
var process = Ti.Process.createProcess({
args:['java', '-cp', 'C:/.../MyProgram.jar', 'package.MyMainClass'],
stdin: input
});
//process.setOnReadLine(function(line) { alert(line) });
process.launch();
input.write("comand parameter1 parameter2\n"); //This line does not work
The java program starts. But never gets a command.
Checkout Documentation of Ti.Process.createProcess. That is exactly what you are looking for:
http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.Process
I usually run this program via a command line like so:
java Program <TestClass.java
Which as I understand, forces the contents of TestClass.java to the console as user input.
i.e. It would be like executing
java Program
and then typing what ever is in TestClass.java
My problem is getting this happening in Eclipse. I can't figure out how to do it.
I would have thought that adding
<TestClass.java
to the program arguments in the run configuration would work, but it seems not.
Any suggestions?
How about adding this on top of your main.
InputStream in;
if (args.length > 0) {
in = new FileInputStream(args[0]);
} else {
// fallback
in = System.in;
}
And then you add the filename as an argument, as if you're running java Program TestClass.java. This way, it will work whether you run it as before or using the filename as an argument.