What does the "< " symbol mean on -jar call? - java

I'm facing a problem with calling java from php on a linux server with popen.
$java = '/usr/bin/java';
$cmd = "$java -jar javafiles/register.jar < $tmpFile";
What does the < before $tmpFile mean? Because apparently it is loading the content of $tmpFile from disk and inputting it directly on the console of the register.jar execution. Is that the case? Because the content of $tmpFile has special characters and these aren't being encoded in the right charset.

Thats exactly what it does. Specifically, it runs the program and sends the contents of $tmpFile into the standard input (System.in) of the program being executed.

Related

IP as Linux array element throws UnknownHostException but as constant works

I have the following script in the directory /home/test/javacall that parses csv of IP pair , invokes a sh file that calls an executable jar to get output from these IPs.
In the below code ip1=${IPArray[0]} throws UnknownHostException from java.
But If I use the ip directly ip1="10.10.10.10" java code works fine. I did System.out.println from java and I got the same IP displayed in both cases. But in the case of ip1=${IPArray[0]} only, I get the exception.
#!/bin/bash
INPUT="IPPairs.csv"
array=()
while IFS="," read var1 var2 ; do
echo $var1 $var2
pairString="$var1***$var2"
array+=("$pairString")
done < $INPUT
for i in "${array[#]}" ; do
echo $i
IPString=$(echo $i | tr '***' ' ')
read -ra IPArray <<< "$IPString"
ip1=${IPArray[0]}
#ip1="10.10.10.10"
ip2=${IPArray[1]}
source /home/test/javacall/javacmd.sh "$ip1" "/home/test/javacall/out.txt" "show running-config all-properties"
done
Exception:
com.jcraft.jsch.JSchException: java.net.UnknownHostException: 10.10.10.10
at com.jcraft.jsch.Util.createSocket(Util.java:349)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
That string (357\273\277) indicates that your csv file is encoded with a Byte-Order Mark (BOM) at the front of the file. The read command is not interpreting the BOM as having special meaning, just passing on the raw characters, so you see them as part of your output.
Since you didn't indicate how your source file is generated, you may be able to adjust the settings on that end to prevent writing the BOM, which is optional in many cases. Alternatively, you can work around it various ways on the script side. These questions both offer some examples:
How can I remove the BOM from a UTF-8 file?
Cygwin command not found bad characters found in .bashrc 357\273\277
But honestly, if you just follow Charles Duffy's advice and run your file through dos2unix before parsing it, it should clean this up for you automatically. i.e.:
...
array=()
dos2unix $INPUT
while IFS="," read var1 var2 ; do
...
Or, building on Charles' version:
#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac
INPUT="IPPairs.csv"
declare -A pairs=( )
dos2unix $INPUT
while IFS=$',\r' read -r var1 var2 _ ; do
pairs[$var1]=$var2
done <"$INPUT"
for ip1 in "${!pairs[#]}"; do
ip2=${pairs[$ip1]}
# Using printf %q causes nonprintable characters to be visibly shown
printf 'Processing pair: %q and %q\n' "$ip1" "$ip2" >&2
done
Do note that running dos2unix in your script is not necessarily the best approach, as the file only needs to be converted once. Generally speaking, it shouldn't hurt anything, especially with such a small file. Nonetheless, a better approach would be to run dos2unix as part of whatever process pushes your csv to the server, and keep it out of this script.
System.out.println() only shows visible characters.
If your input file contains DOS newlines, System.out.println() won't show them, but they'll still be present in your command line, and parsed as part of the IP address to connect to, causing an UnknownHostException. Converting it to a UNIX text file, as with dos2unix, or using :set fileformat=unix in vim, is typically the quickest way to fix this.
BTW, if you don't need ordering retained, an associative array is typically a more appropriate data structure to use to store pairs:
#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac
declare -A pairs=( )
while IFS=$',\r' read -r var1 var2 _ ; do
pairs[$var1]=$var2
done <"$input"
for ip1 in "${!pairs[#]}"; do
ip2=${pairs[$ip1]}
# Using printf %q causes nonprintable characters to be visibly shown
printf 'Processing pair: %q and %q\n' "$ip1" "$ip2" >&2
done
In the above, using IFS=$',\r' prevents LF characters (from the "CRLF" sequence that makes up a DOS newline) from becoming either part of var1 or var2. (Adding an _ placeholder variable to consume any additional content in a given line of the file adds extra insurance towards this point).

How to handle file input in java when the file is provided using < operator and the output is written to a file using > operator?

I want to upload a program on Codechef and the way they test our code is :
java test < in.txt > out.txt
How to get that file in code ... Initially I assumed it to be in arg[0] command-line argument but that is not working. Also please explain how to output the result using >.
Thank you.
The < and > operators redirect standard input and standard output respectively, so given
java test < in.txt > out.txt
you will receive the contents of in.txt via System.in, and anything you write to System.out will go into out.txt.

How to pass argument to Java Process?

I have a Java application that creates and runs a process (x.exe). When process x is executed, it prompts the user to enter a password. How to pass this password to the user?
I tried to use the OutputStream of the process and wrote the password to the stream but still the process didn't run.
Runtime r=Runtime.getRuntime();
Process p=r.exec("x.exe");
p.getOutputStream()//.use to pass the arguments
You need to flush the stream, and also, it maybe expects a CR at the end of the password to simulate the ENTER key the user types at the end of the password. This works for me in Linux:
Runtime r = Runtime.getRuntime();
Process p = r.exec("myTestingExe");
p.getOutputStream().write("myPassword\n".getBytes()); // notice the `\n`
p.getOutputStream().flush();
Some caveats:
This works in Linux with '\n' at the end, maybe in Windows you would need \r instead (honestly I'm not sure of how Windows handles the "ENTER" key in the input)
I'm using "myPassword\n".getBytes() but a more complete value would be new String("myPassword".getBytes(), Charset.forName("MyCharsetName")); (where "MyCharsetName" is a supported encoding) if you are using an encoding like "UTF-8".
As already was pointed out you can consider to use an Expect-like library for interacting between your Java program and a spawn OS process. Basically, you would need to wait until the password prompt gets available in the process input stream and then write the password terminated by the end-of-line to the process output stream.
If you decide to go with a third party library approach I'd recommend you to give a try my own modern alternative to expect4j and others. It is called ExpectIt has no dependencies and is Apache licensed.
Here is a possible example with the use of the expect library:
Process process = Runtime.getRuntime().exec("...");
Expect expect = new ExpectBuilder()
.withInputs(process.getInputStream())
.withOutput(process.getOutputStream())
.withErrorOnTimeout(true)
.build();
expect.expect(contains("Password:"));
expect.sendLine("secret");
expect.close();
Note: the contains method is statically imported.
You can try library like expect4j to interact with the external process

Make a Java Call from within Progress 4gl

Currently, I have a batch file that is basically running an executable jar.
Like this...
java -jar foo.jar
I have code in progress that is executing that batch file and piping out the values it returns into a txt document. I am then reading in that text document and parsing the info accordingly.
However, this is an ugly way of handling this and could lead to many issues down the road. I am basically just looking for a way in progress to execute a os-command and retrieve it's results without writing it to a file and reading back in.
I am running OpenEdge 10.1C
DEFINE INPUT PARAMETER iJarInput AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER oJarOutput AS CHARACTER NO-UNDO.
DEFINE VARIABLE cOut AS CHARACTER NO-UNDO.
DEFINE VARIABLE cCmd AS CHARACTER NO-UNDO.
ASSIGN
cCmd = batchFile + " " + iJarInput.
OS-COMMAND SILENT VALUE(cCmd).
INPUT FROM VALUE(outFile).
REPEAT:
IMPORT UNFORMATTED cOut.
oJarOutput = oJarOutput + cOut.
END.
You can call external shared libraries.
http://documentation.progress.com/output/OpenEdge112/oe112html/ABL/wwhelp/wwhimpl/common/html/wwhelp.htm#href=Programming%20Interfaces/15dvpinch08epi.089.5.html&single=true
You could, for instance, use that capability to create a "shim" to your JAR.

how to display registered trademark symbol ® in unix

How to display registered trademark symbol ® unix environment.
In this symbol working in window environment, but it not working unix environment.I am having paragraph in java file.displaying this symbol when working in windows environment, but move to unix(server) symbol not displaying in the format(®).
String str="stackoverflow ® ";
This is message sending email to customers.When running program in windows sending email correct format.But when running in unix(server) not sending correct format.
What is problem here ?
When sending email from window system that email displaying correct format.But not unix system.
Example:
original message="stackoverflow ®"
Getting email message="stackoverflow ÿ"
What is resaon behind in this ?
this sending email message.
I suspect that the problem is in the way that you are constructing and / or sending the mail message; e.g. in the content type / character set that you are specifying. But we can only guess, unless you post the relevant code ...
As #Paŭlo Ebermann suggests, if you are embedding a special character directly in your code, then you can run into compilation problems if the compiler reads your source code using the wrong character set / encoding. You can avoid these problems entirely by using Unicode escapes; e.g. change your code to say:
String message = "stackoverflow \u00AE";
instead of
String message = "stackoverflow ®";
IMO, this is a good idea ... even if this is not what is causing your problem.
One possible cause could be the email construction, as Stephen C said.
Another possible cause would be the compilation - the compiler uses the default character set of your system if you don't indicate something else, and this default character set could be different on windows and your unix system.
Look how the source file is encoded, and supply this with the -encoding parameter to the compiler.
Alternatively, to be encoding independent, you could write \u00AE in the source code instead.

Categories