I want to ping a target IP address and receive a response. To achieve this, I'm using windows command line in Java with runtime.exec method and process class. I'm getting the response using inputStreamReader.
My default charset is windows-1254, it's Turkish. When I receive it, the response contains Turkish characters but Turkis开发者_如何学Ch characters are not displayed correctly in the console.
I want to get a numeric value from the response I get but the value that I am searching for contains some Turkish characters, so when I look it up, I can't find it.
The codes are below, what I need to know is how to get the Turkish characters visible here:
runtime = Runtime.getRuntime();
process = runtime.exec(pingCommand);
BufferedReader bReader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF8"));
String inputLine;
while ((inputLine = bReader.readLine()) != null) {
pingResult += inputLine;
}
bReader.close();
process.destroy();
System.out.println(pingResult);
In order to fix this, checking the charset that your current operating system uses on its command line and getting the data compatible with this charset is necessary.
I figured out that charset for Turkish XP command line is CP857, and when you edit the code like below, the problem is solved.
BufferedReader bReader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "CP857"));
Thx for your help.
Note : You can learn your default command line charset by "chcp" command.
Are you saying the characters are being retrieved properly but System.out isn't printing them right? System.out is definitely an OLD class. Maybe try
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out,"charactersethere"));
out.println(“encoded system”);
out.flush();
out.close();
That MIGHT work
If you just need to ping and get the response time, then reading the output from the console might be overkill. There's an easier way so long as you're using Java5 or newer:
Here is a complete program that you can use to do this. NOTE: On Unix/Linux/Mac OS, you have to run this program under "sudo" in order to get a response from anything other than "localhost".
import java.net.InetAddress;
import java.io.IOException;
class PingTest {
public static void main(String[] args) {
try {
String hostnameOrIP = args[0];
int timeout = Integer.parseInt(args[1]);
int pingCount = Integer.parseInt(args[2]);
System.out.println("Pinging '" + hostnameOrIP + "'");
for (int i = 0; i < pingCount; i++) {
System.out.println("Response time: " + getPingResponseTime(hostnameOrIP, timeout));
}
} catch (Exception e) {
System.out.println("Usage: java PingTest <hostname/IP address> <timeout in milliseconds> <number of pings to send>\n");
}
}
static long getPingResponseTime(String hostnameOrIP, int timeout) {
long startTime = System.currentTimeMillis();
boolean successfulPing = false;
try {
successfulPing = InetAddress.getByName(hostnameOrIP).isReachable(timeout);
} catch (IOException ioe) {
successfulPing = false;
}
long endTime = System.currentTimeMillis();
long responseTime = endTime-startTime;
if (successfulPing == false)
responseTime = -1;
return responseTime;
}
}
Here are the results that I got back when I ran the following on Mac OS (results are in milliseconds):
$ sudo java PingTest google.com 5000 5
Pinging 'google.com'
Response time: 419
Response time: 15
Response time: 15
Response time: 16
Response time: 16
Reponse times may vary between runs, but I'm seeing < 20 millisecond response times to most major sites, especially if you run multiple pings
System.out(...) - and the Java console - is quite limited in encoding. You can expect basic ASCII characters to work, and that's about all. If you want to use any other encoding, then you should be writing the output to a text file or to a GUI. If you write to the console you'll always have to cope with poor handling of various non-ASCII characters.
You need to change your eclipse default encoding setting under preferences. It can be set to UTF8
.
Try this command line command:
chcp
My command line answered 866 so I used CP866
精彩评论