I am trying to run the following code using a batch file of URLs, while the URL is not null. It runs exactly how I want it to, however, the last line of output reads:
Malformed URL Exception: 'null' is not a valid input
Batch Program Completed Successfully
I want the program to stop right before it actually reads the null. Any suggestions?
(Also, I am still learning how to ask questions without people getting mad at me so if I can improve my question, just let me know instead of down voting. Thanks.)
private static String getBatchUrlContents(String filePath) {
logger.debug(">>getBatchUrlContents()");
String theUrl = "";
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedUrlReader = new BufferedReader(fileReader);
do {
try {
theUrl = bufferedUrlReader.readLine();
URL url = new URL(theUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
String contentType = urlConnection.getContentType();
logger.info("Content Type: {}", contentType);
int statusCode = urlConnection.getResponseCode();
logger.info("Status Code: {}", statusCode);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
List<String> contents = new ArrayList<String>();
while ((line = bufferedReader.readLine()) != null) {
contents.add(line);
}
bufferedReader.close();
String[] contentArray = contents.toArray(new String[contents.size()]);
System.out.println("\n ~~~~~~~NEW URL~~~~~~~ \n" + theUrl);
for (String x :contentArray)
System.out.println(x);
String firstLine = contentArray[0];
if (firstLine.equals("#EXTM3U")) {
logger.info("First line is validated: {}", firstLine);
System.out.println("First line of content is validated: " + firstLine);
} else {
logger.error("Error: First line not valid: {}", firstLine);
System.out.println("Error: First line reads: '" + firstLine + "'\n"
+ "Should read: '#EXTM3U'");
}
} catch(Exception e) {
if (e instanceof MalformedURLException) {
logger.error("Malformed URL Exception: {}", e.toString());
System.out.println("Malformed URL Exception: '" + theUrl + "' is not a valid input");
} else if (e instanceof FileNotFoundException) {
logger.error("404: File Not Found Exception: {}", e.toString());
System.out.println("Unable to open URL: " + theUrl);
} else if (e instanceof IOException) {
logger.error("IO Exception: {}", e.toString());
System.out.println("Error reading file: " + theUrl);
} else {
logger.error("Exception: {}", e.toString());
System.out.println("Exception Error - Unable to read or open: " + theUrl);
}
}
} while (theUrl != null);
bufferedUrlReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + filePath + "'");
System.exit(2);
} catch (IOException ex) {
System.out.println("Error reading file '" + filePath + "'");
System.exit(3);
}
logger.debug("<<getUrlContents()");
return "Batch Program Completed Successfully";
}
The theUrl != null check happens at the end of the loop; it isn't checked continuously.
If you need theURL to be non-null, you'll need to check for that.
You could do the assignment in the condition (although this is frowned upon in some circles):
while((theUrl = bufferedUrlReader.readLine()) != null) {
URL url = new URL(theUrl);
...
or, just do a check inside the loop and do a manual break:
while(true) {
theUrl = bufferedUrlReader.readLine();
if (!theURL) { // Or "if (theURL == null)"
break;
}
URL url = new URL(theUrl);
...
Related
I'm retrieving files list from FTPClient using ftpClient.listFiles(); and it returns an array with correct file names and appropriate quantity.
I'm trying to read content from files, but only ftpClient.retrieveFileStream(fileName) is available for that purpose. And it somehow breaks after reading the first file and returns null.
Is there a way to convert FTPFile directly into String?
ftp.connect();
ftp.changeWorkingDirectoryToFrom();
List<String> idsList = new ArrayList<String>();
List<String> names = ftp.listFileNames();
for (String fileName : names) {
String content = fromFTPFileToString(fileName);
Matcher matcher = FILES_PATTERN.matcher(content);
String id = extractId(content);
if (matcher.find()) {
boolean duplicate = idsList.contains(id);
LOG.info("MATCHED: " + fileName);
if (!duplicate) {
ftp.moveFileFromTo(fileName);
idsList.add(id);
} else {
LOG.info("DUPLICATE: " + fileName);
duplicated++;
ftp.deleteFileOnFromFtp(fileName);
}
}
processed++;
}
ftp.disconnect();
private String fromFTPFileToString(String fileName) {
String content = "";
try {
InputStream is = ftp.readContentFromFTPFile(fileName);
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, ENCODING);
content = writer.toString();
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(writer);
} catch (Exception ex) {
LOG.error(ex);
}
return content;
}
void deleteFileOnFromFtp(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
deleteFile(fileName);
}
InputStream readContentFromFTPFile(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
InputStream inputStream = null;
try {
inputStream = ftpClient.retrieveFileStream(fileName);
} catch (IOException ex) {
LOG.error("Unable to extract content from file:" + O_Q + fileName + C_Q);
}
return inputStream;
}
void moveFileFromTo(String fileName) {
String from = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_FROM.toString()) + FORWARD_SLASH + fileName;
String to = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_TO.toString()) + FORWARD_SLASH + fileName;
try {
ftpClient.rename(from, to);
} catch (IOException ex) {
LOG.error("Unable to move file file from:" + O_Q + from + C_Q + " to: " + O_Q + to + C_Q);
throw new RuntimeException(ex.getCause());
}
}
3 Hints
1) You can use retrieveFile
And use a ByteArrayOutputStream as the 2nd parameter.
To get a String from it simply "new String(baos.toByteArray(),[The Charset you used]);"
2) Check for ftpClient.completePendingCommand();
If some commands are still pending
3) I had once a similar issue setting ftp.enterLocalPassiveMode() helped
I am sending xml data to server via socket programming. I have found that sometimes when the server is down and the client reports socket timeout, I am unable to send or receive.
I want to handle this exception and try to resend for 3 to 4 times. Should I use Thread.sleep, write in loop, or is there any better approach?
private String sendRequestToChannel(String request) {
String xmlData = null;
BufferedReader rd = null;
BufferedWriter bw = null;
String line = null;
String lineSep = null;
String data = null;
StringBuffer serverData = null;
try {
Socket cliSocket = new Socket();
cliSocket.connect(new InetSocketAddress(HOST, PORT), SOCKET_TIMEOUT);
bw = new BufferedWriter(new OutputStreamWriter(cliSocket.getOutputStream()));
bw.write("POST " + PATH + " HTTP/1.0\r\n");
bw.write("Host: " + HOST + "\r\n");
bw.write("Content-Length: " + request.length() + "\r\n");
bw.write("Pragma: cache\r\n");
bw.write("Cache-Control: private, must-revalidate\r\n");
bw.write("Content-Type: application/x-www-form-urlencoded\r\n");
bw.write("\r\n");
bw.write(request);
bw.flush();
rd = new BufferedReader(new InputStreamReader(cliSocket.getInputStream()));
System.out.println("Step 4 : Getting Input Stream");
serverData = new StringBuffer("");
lineSep = System.getProperty("line.separator");
while ((line = rd.readLine()) != null) {
serverData.append(line);
serverData.append(lineSep);
}
data = serverData.toString();
int index = data.indexOf("<");
if (index != -1) {
xmlData = data.substring(index);
} else {
System.out.println("\r\n \r\n XML Data Not Retrived");
}
} catch (java.net.UnknownHostException uh) {
uh.printStackTrace();
System.out.println("$$$$$$$$$$$$ in sendRequestToChannel : UnknownHostException " + uh.getMessage());
return " in sendRequestToChannel : UnknownHostException " + uh.toString();
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("$$$$$$$$$$$$ in sendRequestToChannel : IOException " + ioe.getMessage());
return " in sendRequestToChannel : IOException " + ioe.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("$$$$$$$$$$$$ in sendRequestToChannel : Exception " + e.getMessage());
return " in sendRequestToChannel : Exception " + e.toString();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ex) {
Logger.getLogger(SA_Caesar.class.getName()).log(Level.SEVERE, null, ex);
}
try {
if (rd != null) {
rd.close();
}
} catch (IOException ex) {
Logger.getLogger(SA_Caesar.class.getName()).log(Level.SEVERE, null, ex);
}
bw = null;
rd = null;
line = null;
lineSep = null;
data = null;
serverData = null;
}
return xmlData;
}
The biggest problem an application faces when its remote service goes down is that there's no way at all to predict when it will return, if ever.
You are already using a TCP connection which will retry when faced with short-term unreachable services, but by the time TCP has declared the connection dead, you actually don't increase reliability by automatically trying to re-establish the connection. For example, if it takes two days for the remote server to come back, will your application be able to act as if it had never been down? Will all necessary data be queued during the outage? Will the semantics of a connected system be preserved across a weekend's failure? How about if the remote service is down for 5 days?
The best that you can do from a system perspective is notify the operator that there is a fault which requires attention. This is why we still have operators and will for the foreseeable future.
When I try to get information from a Socket (it's at localhost) it never returns what I want (info from my server) with php it works fine..
Java application: run & debug doesn't return anything
Glassfish server: run doesn't return anything, debug return all the info, everytime i debug
PHP Code:
$Socket = fsockopen($this->Host, $this->Port, $errno, $errstr, 5);
if(!$Socket) {
return false;
} else {
stream_set_timeout($Socket, 1);
stream_set_blocking($Socket, false);
fwrite($Socket, chr(6).chr(0).chr(255).chr(255).'info');
while(!feof($Socket)) {
$this->SocketData .= fgets($Socket, 8192);
}
fclose($Socket);
return true;
}
Java Code:
public static String serverInfo(String ip, Integer port) {
try (Socket socket = new Socket(ip, port)) {
//System.out.println("Connected to " + socket.getInetAddress() + " on port " + socket.getPort() + " from port " + socket.getLocalPort() + " of " + socket.getLocalAddress());
OutputStream os = socket.getOutputStream();
String info = ((char) 6 + "" + (char) 0 + "" + (char) 255 + "" + (char) 255 + "info");
System.out.println(info);
os.write(info.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder stb = new StringBuilder();
while (br.ready()) {
stb.append(br.readLine());
}
if (!stb.toString().isEmpty()) {
//System.out.println("Info: " + stb.toString());
return stb.toString();
} else {
//System.out.println("No answer from server.");
return null;
}
} catch (UnknownHostException e) {
System.err.println("I can't find " + ip);
} catch (SocketException e) {
System.err.println("Could not connect to " + ip);
} catch (IOException e) {
System.err.println(e);
}
return null;
}
What I was looking for, was to do the same as php does, connect to the socket, get the information and return/save/whatever with that information and I don't know what happens that only 1/100 times that I try, it returns something.
When I run in my web application AND debug, it returns EVERYTIME the info, something is really wrong and I can't figure it out..
PS: It's meant to get OTServer information.
I think this is not the right way to read from the br.
while (br.ready()) {
stb.append(br.readLine());
}
Try changing this to:
String line = null;
while ((line = br.readLine()) != null) {
stb.append(line);
}
Also, call os.close() after this line.
os.write(info.getBytes());
I have a Junit / Silkuli test that runs on windows 7 and is dependent on an external program already running.
How can I check if the external program is running from inside the test?
You can run command "tasklist" from within java and look at the returned list to see if your program is in the list. something like:
String program = "eclipse.exe"; //or any other process
String listOfProcesses = getCommandOutput("tasklist");
if (listOfProcesses == null || listOfProcesses.isEmpty()) {
System.err.println("Unable to automatically determine if " + program + " is running");
} else {
if (listOfProcesses.contains(program)) {
System.out.println(program + " is running!");
} else {
System.out.println(program + " is not running!");
}
}//else: process list can be retrieved
and get the output from a command using a method such as:
public static String getCommandOutput(String command) {
String output = null; //the string to return
Process process = null;
BufferedReader reader = null;
InputStreamReader streamReader = null;
InputStream stream = null;
try {
process = Runtime.getRuntime().exec(command);
//Get stream of the console running the command
stream = process.getInputStream();
streamReader = new InputStreamReader(stream);
reader = new BufferedReader(streamReader);
String currentLine = null; //store current line of output from the cmd
StringBuilder commandOutput = new StringBuilder(); //build up the output from cmd
while ((currentLine = reader.readLine()) != null) {
commandOutput.append(currentLine + "\n");
}
int returnCode = process.waitFor();
if (returnCode == 0) {
output = commandOutput.toString();
}
} catch (IOException e) {
System.err.println("Cannot retrieve output of command");
System.err.println(e);
output = null;
} catch (InterruptedException e) {
System.err.println("Cannot retrieve output of command");
System.err.println(e);
} finally {
//Close all inputs / readers
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
System.err.println("Cannot close stream input! " + e);
}
}
if (streamReader != null) {
try {
streamReader.close();
} catch (IOException e) {
System.err.println("Cannot close stream input reader! " + e);
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println("Cannot close reader! " + e);
}
}
}
//Return the output from the command - may be null if an error occured
return output;
}
I am working on an Editor application where I can compile and run c,cpp and Java file.I am developing this application using java programming language.I am developing it in Eclipse.
I am able to create new files(c,cpp and java) on specific locations and also I am able to save file to different-2 locations.
And for execution I am using following methods.
String compileFileCommand = "javac "+fileName;
Process compile_process = new ProcessBuilder(compileFileCommand).redirectErrorStream(true).start();
compile_process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(compile_process.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}
My problem is that I am not able to compile files from their corresponding locations.
Always giving Exception
java.io.IOException: error=2, No such file or directory
Please tell me how can I compile and run all c,c++ & java files.
Please also give me any other suggestion for my application.
Edited ..
I have used these two methods for compiling and running.On Compiling it creates a class file in case of Java.But all the time I am getting null from InputStreams(both getErrorStream() and getInputStream()).
void compileJavaFile(String fileName)
{
String compileFileCommand = "javac " + fileName;
try
{
System.out.println("Executing Java File");
Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);
String line = "";
BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
while ((line = bri.readLine()) != null)
{
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null)
{
System.out.println(line);
}
bre.close();
compileProcess.waitFor();
System.out.println("Done.");
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runJavaFile(String fileName)
{
String runFileCommand = "java " + fileName.split(".java")[0];
try
{
System.out.println("runFileCommand : " + runFileCommand);
System.out.println("Running Java File");
Process runProcess = Runtime.getRuntime().exec(runFileCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String line = reader.readLine();
System.out.println("line = " + line);
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
And for C and C++ I am using.
void compileCFile(String fileName)
{
String compileFileCommand = "gcc " + fileName;
resultString = "";
try
{
System.out.println("Compiling C File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".c")[0];
try
{
System.out.println("Running C File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void compileCPPFile(String fileName)
{
String compileFileCommand = "g++ " + fileName;
try
{
System.out.println("Compiling CPP File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCPPFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".cpp")[0];
try
{
System.out.println("Running CPP File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
In case of C and C++ it show error like
g++: /media/disk/eclipse/\/UniversalIDE/CPP/firstCPP: No such file or directory
Please give me solution for my problems ..
Please try following,
Process p = Runtime.getRuntime().exec(command);
command is a string you pass. in command you can pass "javac Test.java" to compile your java file & just like that you can use other commands.
replace the line
String runFileCommand = "./" + fileName.split(".c")[0];
with
String runFileCommand = "./a.out";