filenotfound issues in eclipse on Mac OSX - eclipse

Hi
I have a java class which is working fine in windows but not in Mac OSX snow leopard. I am using Eclipse on both operating systems. On Mac OSX its throwing file not found exception.
Basically I am trying to read a file using BufferedReader and FileReader and I put my file in \resources\
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileContents {
/**
* #param args
*/
public static void main(String[] args) {
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader("resources\\abc"));
while((line = br.readLine())!= null)
{
System.out.println("Read ::: "+line+" From File.");
}
} catch (FileNotFoundException fne) {
fne.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
On Mac it is giving
java.io.FileNotFoundException: resources\abc (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at ReadFileContents.main(ReadFileContents.java:18)
Do I need any special configuration in my eclipse to get this working...

Mac uses forward slashes: "resources/abc". (This will actually work on Windows as well. Only the command line interpreter requires backslashes.)

Related

Getting Exception while calling WebDAV propFindMethod in java

Getting below Exception::
WARN main [DavDocumentBuilderFactory.createFactory] - Secure XML processing is not supported
java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.createFactory(DavDocumentBuilderFactory.java:50)
at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.<init>(DavDocumentBuilderFactory.java:39)
at org.apache.jackrabbit.webdav.xml.DomUtil.<clinit>(DomUtil.java:60)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:203)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:108)
at com.spero.poc.WebDavClient.main(WebDavClient.java:92)
Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:377)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:131)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:684)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:728)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:343)
at org.apache.jackrabbit.webdav.xml.DomUtil.transformDocument(DomUtil.java:805)
at org.apache.jackrabbit.webdav.client.methods.XmlRequestEntity.<init>(XmlRequestEntity.java:43)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:193)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:205)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:109)
at com.spero.poc.WebDavClient.main(WebDavClient.java:92)
Here is my code::
package com.spero.poc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
public class WebDavClient {
static HttpClient client = null;
public static void main(String[] args) {
// String uri = "https://www.webdavserver.com/Userab71566/Library/Content.txt";
String uri = "https://www.webdavserver.com/Userab71566/Library/";
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("https://www.webdavserver.com");
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
int maxHostConnections = 20;
params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
params.setConnectionTimeout(60000);
connectionManager.setParams(params);
HttpClientParams cParams = new HttpClientParams();
cParams.setSoTimeout(60000);
// Create the HttpClient object and eventually pass the Credentials if required:
client = new HttpClient(cParams,connectionManager);
client.setHostConfiguration(hostConfig);
// Credentials creds = new UsernamePasswordCredentials("userId", "pw");
// client.getState().setCredentials(AuthScope.ANY, creds);
try {
WebDavClient test = new WebDavClient();
test.listOfFilesFromWebDav(uri);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
client = null;
}
if(client == null)
System.out.println("Client is in null state");
}
private List<String> listOfFilesFromWebDav(String folderUrl) {
try {
PropFindMethod pFind = new PropFindMethod(folderUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
pFind.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
client.executeMethod(pFind);
List<String> files = new ArrayList<>();
MultiStatus multiStatus = pFind.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();
for (MultiStatusResponse response : responses) {
if (response.getHref().endsWith(".txt")) {
System.out.println("Data::"+response.getHref());
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DavException e) {
e.printStackTrace();
}
return null;
}
}
What version of Jackrabbit?
Looking at the source I see that the exception is actually caught and logged, and the system should proceed.
See https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DavDocumentBuilderFactory.java#L43
Are you really getting an exception in the code?
Thanks for the suggestion, I have gone through the logs and find out the mismatched class and jars. I have added the below system settings to my code and the Jar.
We have to resort to System.setProperty(...) calls from your code, eg:
//this would enforce the DOM implementation to be Xerces
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
//this would enforce the SAX implementation to be Xerces
System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl");
//this would enforce the XSLT implementation to be Xalan
System.setProperty("javax.xml.transform.TransformerFactory","org.apache.xalan.processor.TransformerFactoryImpl");
The respective JRE implementation classes are:
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

I tried using s_client (openssl) and the javax.net.ssl package to connect to encrypted socket

I tried using s_client (openssl) and the javax.net.ssl package to connect to encrypted socket.
s_client works but probably not on non-rooted android, that is why i tried the javax.net.ssl package but i get the following error:
java.net.SocketException: Socket has been closed or broken
Also, what is "NaiveTrustManager()"? (https://github.com/maruohon/JavaIRC)
package SSLSocketClient;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
public class SSLSocketClient{
public static void main(String[] args) {
SSLSocketFactory f =
(SSLSocketFactory)SSLSocketFactory.getDefault();
try {
SSLSocket s =
(SSLSocket) f.createSocket("localhost", 6697);
s.startHandshake();
BufferedReader reader = new BufferedReader (new
InputStreamReader(s.getInputStream()));
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
s.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}
I solved it by Accepting Self-Signed SSL Certificates in Java..
http://howardism.org/Technical/Java/SelfSignedCerts.html

Unable to execute jar file in a ubuntu server

I have a small sqoop import project where i have to import from mysql to hdfs. It is a maven project, while i am running in my local system it works fine. Now when i am trying to run in my ubuntu server by running a jar file, it is executing but not showing any output. It is not a web based application, normal application where i am running hadoop deamons and running this project. Tell me how to run this type of projects in servers and run.
package org.ezytruk.sqoop;
import java.io.FileInputStream;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.sqoop.tool.ImportTool;
import com.cloudera.sqoop.SqoopOptions;
import com.cloudera.sqoop.SqoopOptions.FileLayout;
public class SqoopImport {
public static void importMySQLToHDFS() throws Exception {
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
Configuration config = new Configuration();
config.addResource(new Path("/usr/local/hadoop/etc/hadoop/conf/core-site.xml"));
config.addResource(new Path("/usr/local/hadoop/etc/hadoop/conf/hdfs-site.xml"));
Properties properties = new Properties();
properties.load(new FileInputStream("/home/*****/SqoopImportHDFS/resources/sqoopImport.properties"));
//org.apache.sqoop.SqoopOptions sqoopOptions = new org.apache.sqoop.SqoopOptions();
SqoopOptions sqoopOptions = new SqoopOptions();
sqoopOptions.setDriverClassName(driver);
sqoopOptions.setHadoopHome("/usr/local/hadoop");
sqoopOptions.setConnectString(properties.getProperty("db_mysql_connection_string"));
sqoopOptions.setTableName(properties.getProperty("db_mysql_table_name"));
sqoopOptions.setUsername(properties.getProperty("db_mysql_username"));
sqoopOptions.setPassword(properties.getProperty("db_mysql_password"));
sqoopOptions.setNumMappers(1);
//sqoopOptions.setWarehouseDir(properties.getProperty("db_hdfs_warehouse_directory"));
sqoopOptions.setTargetDir(properties.getProperty("db_hdfs_output_directory"));
sqoopOptions.setFileLayout(FileLayout.TextFile);
new ImportTool().run(sqoopOptions);
} catch (Exception e) {
}
}
public static void main(String[] args) throws Exception {
SqoopImport mySqlImport = new SqoopImport();
mySqlImport.importMySQLToHDFS();
}
}

Raspberry PI Tomcat7 and Serial Port Communication using ISO8859-7

I am trying to relay a message from a Servlet to COM in raspberry pi on Tomcat 7.
I am using null cable between raspberry and my PCs to test.
I am using jssc API (Java Simple Serial Connector) for serial communication.
Raspberry pi is using JDK 1.8.0_65.
I am getting the message in UTF8 and I should output it in ISO8859-7.
Since UTF8 is a superset of ISO8859-7, the app that calls the servlet ensures all characters sent are legitimate for ISO8859-7.
My code:
package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import jssc.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(value = "/Relay", name = "Relay")
public class Relay extends HttpServlet {
static Logger app = null;
static {
app = Logger.getLogger("com.test.app");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) {
try {
request.setCharacterEncoding("ISO-8859-7");;
response.setCharacterEncoding("ISO-8859-7");
//request.setCharacterEncoding("UTF-8");
//response.setCharacterEncoding("UTF-8")
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String message = request.getParameter("message");
app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", message);
String[] portNames = SerialPortList.getPortNames();
app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", portNames.length+"");
for(int i = 0; i < portNames.length; i++){
applogp(Level.INFO, this.getClass().getCanonicalName(),"APP", portNames[i]);
byte[] msg = new byte[1024];
msg = message.getBytes("ISO-8859-7");
Charset utf8charset = Charset.forName("UTF-8");
Charset iso88597charset = Charset.forName("ISO-8859-7");
ByteBuffer inputBuffer = ByteBuffer.wrap(message.getBytes());
CharBuffer data = utf8charset.decode(inputBuffer);
ByteBuffer outputBuffer = iso88597charset.encode(data);
byte[] outputData = outputBuffer.array();
byte[] b1 = message.getBytes();
byte[] b2 = message.getBytes(Charset.forName("ISO-8859-7"));
byte[] b3 = message.getBytes(StandardCharsets.ISO_8859_1);
SerialPort serialPort = new SerialPort((portNames[i]));
try {
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
serialPort.writeBytes(msg);
serialPort.writeBytes(message.getBytes());
serialPort.writeBytes(outputData);
serialPort.writeBytes(b1);
serialPort.writeBytes(b2);
serialPort.writeBytes(b3);
serialPort.closePort();
} catch (SerialPortException ex) {
app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", ex.getMessage());
out.write("NOK");
out.close();
}
}
out.write("OK");
out.close();
} catch (IOException e) {
app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", e.getMessage());
}
}
private static final long serialVersionUID = 1L;
}
The problem is that when I am testing I do not get valid output in putty.
putty output
I have configured putty to display ISO8859-7 characters.
Any for changes ?
What am I missing ?
Thanks in advance.
I tried to divide the problem by producing the following code:
import java.io.UnsupportedEncodingException;
import jssc.SerialPort;
import jssc.SerialPortException;
public class SerialTest {
public static void main(String[] args) {
String message = "message μήνυμα";
if ( sendTextOnCom(message) ) {
System.out.println("SUCCESS MESSAGE SENT");
}
else{
System.out.println("FAIL MESSAGE NOT SENT");
}
}
private static boolean sendTextOnCom(String message) {
boolean isOverlaid = false;
SerialPort com = null;
try {
String comNo = "COM1"; // String comNo="/dev/ttyUSB0"; //when used in Raspberry
com = new SerialPort(comNo);
com.openPort();
com.setParams(9600, 8, 1, 0);
com.writeString(message);
com.writeBytes(message.getBytes("ISO-8859-7"));
com.closePort();
isOverlaid = true;
}
catch (SerialPortException ex) {
System.out.println("[ERROR] COM ERROR SENDING MESSAGE");
isOverlaid = false;
try {
com.closePort();
} catch (SerialPortException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isOverlaid;
}
}
The code is working normally in Windows 7 64bit and it is producing output in putty with the right characters.
When I compile and run the same code in raspberry PI the output in putty is not showing the valid characters.
I tend to think that it's a raspberry PI configuration issue.

Where can I find eclipse icons?

I search for a library, a ".zip"-archiv or another easy way to get all "eclipse-icons". I mean the icons on the top of the tabs (Error, Debug, Search, Task and so on..)
Any idea?
Eclipse actually has a special view that shows all available icons:
Window ▸ Show View ▸ Other... ▸ Plug-in Development ▸ Plug-in Image Browser
Do you want to find the plugin they are in, or download similar ones? Try http://eclipse-icons.i24.cc/
I found it was much easier to just write a little program to extract the images. The code below pulls out all *.png from the JAR files in the Eclipse plugins directory.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ExtractEclipseIcons {
public static void main(String[] args) {
String dir="C:\\eclipse\\e47dtp\\eclipse\\plugins";
for (File file : new File(dir).listFiles()) {
if(file.getName().endsWith(".jar")) {
unpackImagesFromJAR(file);
}
}
}
private static void unpackImagesFromJAR(File file) {
try (ZipFile zip = new ZipFile(file)){
for (ZipEntry ze : Collections.list(zip.entries())) {
String name = ze.getName();
if(name.endsWith(".png")) {
try(InputStream in = zip.getInputStream(ze)){
String outname = file.getName()+"/"+name;
File outfile = new File("data/"+outname.replace('/', '_').replace('\\', '_'));
stream2File(in,outfile);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void stream2File(InputStream is, File file) throws IOException {
byte[] buffer = new byte[8 * 1024];
try {
OutputStream output = new FileOutputStream(file);
try {
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
is.close();
}
}
}
Try this tool: http://www.angusj.com/resourcehacker/
It allows you to extract/cange nearly any icon/pic inside an exe file.