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.
Related
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.
So I'm creating a Java program and I want to make it so that you can ask it to open a program.
But, here's the catch, I want the program it opens to be taken from the user input, right now I'm trying to change this
try{Process p = Runtime.getRuntime().exec("notepad.exe");}
catch(Exception e1){}
Into something that opens a program that you asked it to open.
Here's an example of what I want:
User: Can you open chrome?
Program: Of course, here you go!
chrome opens
Could anyone tell me how I would be able to do this?
You can do it in two ways:
1.By Using Runtime:
Runtime.getRuntime().exec(...)
So, for example, on Windows,
Runtime.getRuntime().exec("C:\application.exe -arg1 -arg2");
2.By Using ProcessBuilder:
ProcessBuilder b = new ProcessBuilder("C:\application.exe", "-arg1", "-arg2");
or alternatively
List<String> params = java.util.Arrays.asList("C:\application.exe", "-arg1", "-arg2");
ProcessBuilder b = new ProcessBuilder(params);
or
ProcessBuilder b = new ProcessBuilder("C:\application.exe -arg1 -arg2");
The difference between the two is :
Runtime.getRuntime().exec(...)
takes a single string and passes it directly to a shell or cmd.exe process. The ProcessBuilder constructors, on the other hand, take a varargs array of strings or a List of strings, where each string in the array or list is assumed to be an individual argument.
So,Runtime.getRuntime.exec() will pass the line C:\application.exe -arg1 -arg2 to cmd.exe, which runs a application.exe program with the two given arguments. However, ProcessBuilder method will fail, unless there happens to be a program whose name is application.exe -arg1 -arg2 in C:.
You can try it with like. Pass whole path of where you install chrome.
try{
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
}
catch(Exception e1){
}
When using exec, it is essentially the same as if you were using the command line on windows. Open Command Prompt, type open, and see if it gives details as to how it opens files. If not, find the opener. Usually when dealing with command line operations, there are multiple parameters that are required for opening files/applications. An example of this would be for opening the "TextEdit.app" application on a mac.
Process p = Runtime.getRuntime().exec("open -a TextEdit.app");
Terminal(for mac) would open the app using the -a flag, meaning "application." You could open a file doing:
Process p = Runtime.getRuntime().exec("open filename.file_ext -a TextEdit.app");
The second one will tell the computer to find the application named <app_name>.app and open the file filename.file_ext
I know this is not going to work for a windows machine, but it's only to show how to use the command line operations for opening files and applications. It should be similar for windows though.
Hope this helps
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 am trying to call a perl script from java runtime. It worked fine on my windows7 laptop with the following code,
try {
String cmdString= "c:\\perl64\\bin\\perl.exe c:\\perl64\\eg\\userinput.pl \""+arg1+"\" \""+arg2+"\"";
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
The perl script runs and produces what I expect (update database).
When I move the whole thing over to a remote CentOS server, it doesn't work anymore. The script is the same and the java code is,
try {
String cmdString= "/opt/lampp/bin/perl /home/support/scripts/userinput.pl \""+arg1+"\" \""+arg2+"\" > /tmp/userinput.log";
log(cmdString);
Process p = Runtime.getRuntime().exec(cmdString);
} catch(IOException e) {
System.out.println(e);
}
I added redirect to /tmp/userinput.log after I see the script is not working. But there is no log file created at all. I also added log to make sure this part of the java code did get executed, and indeed it did. I also tried to add "/bin/bash " in front of the comString and it didn't make a difference. However, when I run the cmdString directly on the remote server from command line, it works without problem.
Now, when I changed the cmdString to "touch /tmp/userinput.log", it does create the empty log file.
So I know the Runtime.getRuntime().exec(cmdString) command ran, and the cmdString works when entered on command line, and a simple "touch" command would work with this setup. But I am totally lost why the actual cmdString that calls the perl script doesn't work, and there is no message whatsoever to tell me what is wrong.
Can someone please help?
Frist, separate each parameter for the command and use the version of exec which takes a String[] (you won't have to worry about quoting issues). also, shell redirection won't work since java isn't executing a shell.
I am a new Java programmer. The following is my code:
public static void main(String[] args) throws Exception {
BPM2SampleProcessor processor = new BPM2SampleProcessor();
processor.setSampleSize(1024);
EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);
output.setAverageLength(1024);
Player player = new Player(new FileInputStream(args[0]), output);
player.play();
log.log(Level.INFO, "calculated BPM: " + processor.getBPM());
}
It shows a runtime error as
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 in the following line: Player player = new Player(new FileInputStream(args[0]), output);
Please explain what the error is and how to overcome it.
Are you running your code from the command line or from an IDE like eclipse?
Every main method has a String[] (usually called args) which you can see in the first line of your code.
The program is trying to use args[0] as the name of the file to open. (which you supply from the command line, or configure in the IDE). But right now the args variable doesn't have anything in it. Try replacing args[0] in your program with a string representing the file you want to open. You will have to make sure that you get the correct path.
You're likely not passing any command line parameters into your program when you run it, so the size of args is 0, and args[0] will throw the exception your seeing. The solution is to pass a parameter, presumably an appropriate file name in when you run this program by calling
java MyProgram myfilename.ext
When you run the program you have to give an argument. the args[0] is coming from the user input which you havent given. something like java MyProgram 5
You are not passing the argument required args[0] and hence it is throwing the error.