How to handle "<" in args from terminal input in java [duplicate] - java

I am trying to read the filename by the command line,
This is command that our professor wants us to type:
java MultiBinaryClient xxxxxx.edu 6001 < files.txt
I was trying to use args[3] to get the file name, but args only contains "xxxxxx.edu" and "6001". why not "<" and "files.txt" in the args[]? Can anyone help me out?
BTW, I am using MAC terminal to test my code, I believe my professor uses win CMD, will it make differences?
Thank you!

Let's see what each fragment means. This is how we execute a Java class containing a main method:
java MultiBinaryClient
The only command-line arguments that are being passed to your program are these ones:
xxxxxx.edu 6001
And this snippet is not part of the expected arguments to the Java program:
< files.txt
It's just Unix shell syntax to specify that the contents of files.txt must be read into your program via the standard input.

I know it's an old question, but I've had this problem recently. Here's what I've done to handle it:
Well, as the others said, the "<" redirects the file contents to stdin. If you want to use the file contents as program arguments, you can use xargs:
xargs -a FILE java JAVA_ARGS
or, more specifically:
xargs -a FILE java -cp CLASSPATH CLASS_WITH_MAIN_METHOD

< is a redirection. The file will be streaming over stdin.

You should escape the '<'
java MultiBinaryClient xxxxxx.edu 6001 \< files.txt

Related

Running java executable with powershell within command prompt

I have a powershell script that is working and runs a java executable. Before I was generating a bunch of powershell script files that were run through the command prompt. Now I want to make it so there does not need to be file creation.
Here is what the line looks like from the working (.ps1) file:
java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe
I want to be able to do something like this in command prompt:
Powershell -Command "java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe"
Even just asking this question I am having problems with the escape characters. What is the proper way to handle escape characters when you have quotes in quotes in quotes when calling java through powershell through command prompt? (I understand it is a bit messy)
You can lessen the quoting headaches if you focus just on the parts that require quoting (assuming that option value true truly needs quoting):
REM From cmd.exe
C:\> powershell -Command java -Doption1=3 -Doption2="'\"true\"'" jar.exe
The above will make java.exe see:
java -Doption1=3 -Doption2="true" jar.exe
As you can see, even this simple case of the desired resultant quoting is quite obscure, because you're dealing with 3 layers of interpretation:
cmd.exe's own interpretation of the initial command line
PowerShell's interpretation of the arguments it receives.
How PowerShell's translates the arguments into a call to an external program (java.exe).
In any event, the final layer of interpretation is how the target program (java.exe) parses the command line.
That said, in order to call java.exe, i.e. an external program, you don't need PowerShell at all; invoke it directly:
REM From cmd.exe
C:\> java -Doption1=3 -Doption2="true" jar.exe

Creating a Runnable jar with optional parameters

Curious little issue I am running into here:
I would like a client to be able to do something like:
(1) java -jar myJar.jar inputFile outputFile
or
(2) java -jar myJar.jar text outputFile
outputFile is an optional argument.
Essentially (1) will read input from a file for them, while (2) they provide the input my program will use directly. There is no way to determine whether or not the argument is the input or whether it is the location of the file though that I can think of. For normal command line stuff you would specify a flag like -i inputfile to show you want it to read from a file. What are my options here that maintain ease of use for the client?
Is my only option to create a syntax the client must use for the first argument? i.e.
"-t text" or "-i inputFile"?
I have seen libraries such as commons cli which would enable this, but I would prefer a solution that does not involve using a library.
If I understood the problem correctly, can't you just check the number of arguments instead of explicitly specifying direct input? Like, if one arg is passed the first arg is output, and if 2 args are passed then the first arg is input and the second arg is output?
With that being said - Using a 'option' syntax of the hyphen- kind is probably easiest on most people used to a cli.
I would suggest to read from standard input (System.in) and print to standard out (System.out)
This way, no parameters are required. It's very intuitive for people who work with CLI tools. And you can leave the details of where the input comes from to the OS.
So in a unix OS, you would use your jar something like this:
echo "some test" | java -jar myjar.jar > outfile
or
cat somefile | java -jar myjar.jar > outfile
You can use
input=text or file=inputfile
(with output=outputFile as optional).
In this case, you can treat all your parameters in the same way and split them with the delimiter '=', check the left side of '=' to determine which parameter it is.

Starting a java program from shellscript

I'm complete Linux newbie, but still want to provide a simple way for Linux users to start my Java program.
Therefore I want to create a shellscript.
I can't test my script so I'll have to ask here if this is working correctly:
#!/bin/bash
java -cp "bin";"extres/junit.jar" data.ProgramOne
exit 0
Your mistake is in path delimiter. It is ; on Windows and : on Linux.
Moreover you should not wrap each classpath fragment with "". On unix you can escape spaces and other forbidden characters using \. So, I'd re-write the java execution line as:
java -cp bin:extres/junit.jar data.ProgramOne
This will run when you are executing script from your app directory where you have subdirectory bin and extres.
try this:
java -cp "bin:extres/junit.jar" data.ProgramOne
Java under Unixes uses : as the separator in the classpath, so you'd need (the quotes are not necessary):
#!/bin/bash
java -cp bin:extres/junit.jar data.ProgramOne

Running a Command Prompt from a Java program in Windows

I currently have the following batch script I want to run from my Java program:
"C:\Program
Files\Java\jdk1.6.0_25\bin\java.exe"
-classpath "D:..."
Main >
"...\result.out"
Now, I've done a simple
Runtime.getRuntime().exec(command);
where command is that string I have shown above. The problem is that it is simply calling java.exe with the shown arguments, instead of calling the console with the given arguments. The difference is subtle, for if it is calling directly java.exe it will ignore the redirect of the output stream!
Is there a easy way to do this? I've tried prefixing command with "cmd " but that didn't seem to help.
I'd like to stay away from having to read the output stream and then having to manually save this to a file.
Thanks
To solve the issue,
cmd /c "command"
is enough.
Process proc = Runtime.getRuntime().exec("acpi -b");
Now you can use proc.getInputStream() and proc.getOutputStream() like any normal input and output streams.
You can then write the contents to your output file.
This is the method I mostly use.

Problem with run DOS command in Java

Dear all
I want to execute a EXE file in Java, but I was not able to do it correctly.
Originally, in DOS command prompt, my command is like this:
C:>\crf_test.exe model <inputfile.txt> outputfile.txt
Note: the input file name must be place in the brackets <>. It always gave me good results when run it in DOS window.
When I want my java program to call above command, I do like this:
Process p = Runtime.getRuntime().exec("crf_test.exe model <inputfile.txt> outputfile.txt");
However, the output of this command is "no such file or directory: "
I guest Java doesn't like the brackets <> in DOS command. I also remove <> out, but exe file did not accept that.
So now how can I deal with this problem? Please give me a sollution
Thank you much much
There are potentially two problems. Firstly, the one that others have mentioned - crf_test.exe may not be in your path. It's hard to tell whether your output of "no such file or directory" is from crf_test.exe or from Java trying to find crf_test.exe.
Secondly though, running it from DOS you're not really putting the filename in brackets - you're redirecting system input from the input file to the output file, so the logical grouping is:
crf_test.exe model < inputfile.txt > outputfile.txt
Now when you're running it from Java, it's genuinely trying to pass <inputfile.txt> as a second command-line argument, and outputfile.txt as a third. My guess is that crf_test.exe is then trying to load those as files, and giving "no such file or directory" as an error.
You'll need to load the data from inputfile.txt yourself and pass it in via Process.getOutputStream, and then read the output from Process.getInputStream and write it to outputfile.txt.
Alternatively, you could run cmd.exe and pass in the whole command as an argument - that way the shell will perform the redirection for you as if you were running from a DOS prompt.
The angle brackets are redirection operators: <inputfile.txt causes the input to be read in from inputfile.txt instead of the keyboard, >outputfile.txt causes the output to be written to outputfile.txt instead of the screen. This facility is provided by the shell, however when invoking your program with the Java runtime the shell is not present. Invoke via the shell, like this:
Runtime.getRuntime().exec("cmd /c crf_test.exe model <inputfile.txt> outputfile.txt");
...or redirect input and output using facilities provided by Java; see e.g. this question.
try this
Process p = Runtime.getRuntime().exec("crf_test.exe","/c","model outputfile.txt");
The problem is that the crf_test.ext is missing in your current execution folder. You need either copy the executable or use the absolute path or set the PATH variable to include the necessary path.
If I understand the problem right, you want to call
crf_test.exe model
with input from file inputfile.txt and output to outputfile.txt
You have to redrect in and out and call only crf_test.exe model. How do redirect in and out is described in how to redirect stdin to java Runtime.exec ?

Categories

Resources