add external jars in spring - java - java

I am new to spring, I am trying a simple web dynamic application getting data from database and showing on front end using impala.
This is connector class :
private static final String IMPALAD_HOST = "host";
private static final String IMPALAD_JDBC_PORT = "port";
private static final String CONNECTION_URL = "jdbc:hive2://" + IMPALAD_HOST + ':' + IMPALAD_JDBC_PORT + "/;auth=noSasl";
private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
public Connection getConnection() throws ClassNotFoundException{
Connection con = null;
try {
Class.forName(JDBC_DRIVER_NAME);
con = DriverManager.getConnection(CONNECTION_URL,"","");
}catch (SQLException e) {
e.printStackTrace();
}
return con;
}`
HIve-connector jar is added in the java build path in eclipse. getConnection() works if i call it from a main method of a java class, but getConnection() gives hive driver not found exception if i call this method from jsp page. :
java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver

You are not having the hive-jdbc.jar in your webapplication archive. i.e. war file. it is being missed while packaging the application.You should place it in the WEB-INF/lib directory. Please also ensure that you also add it in the deployment assembly of the eclipse project.
It works when you run the main class because the hive-jdbc.jar is configured in the build path. It is different from webapplication perspective.
Note: ClassNotFoundException shouldn't be thrown unless you are going to handle it. You should have all the required jars in your application package during runtime in classpath.

You are using the wrong Driver-Class.
Use org.apache.hadoop.hive.jdbc.HiveDriverinstead.

Related

Package Spring web project into jar (uber-jar) without spring boot (non spring-boot project)

I have read through some tutorials,
https://spring.io/guides/gs/spring-boot/
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-plugin
I also saw people asking here, but it is using Maven, I tried to use Gradle, but it doesn't work.
I can't really get it works in non Spring-boot project, so my question is, is it possible to package uber-jar in non Spring boot project?
My Spring project is normal MVC project, built by Gradle, is there any Gradle plugin can fulfill my objective? Or actually the Spring-boot plugin can do it on non Spring-boot project?
You can use a embedded tomcat to do that works. May be this article will help you Create a Java Web Application Using Embedded Tomcat
And here is my TomcatBootstrap code
public class TomcatBootstrap {
private static final Logger LOG = LoggerFactory.getLogger(TomcatBootstrap.class);
public static void main(String[] args) throws Exception{
System.setProperty("tomcat.util.scan.StandardJarScanFilter.jarsToSkip", "*.jar");
int port =Integer.parseInt(System.getProperty("server.port", "8080"));
String contextPath = System.getProperty("server.contextPath", "");
String docBase = System.getProperty("server.docBase", getDefaultDocBase());
LOG.info("server port : {}, context path : {},doc base : {}",port, contextPath, docBase);
Tomcat tomcat = createTomcat(port,contextPath, docBase);
tomcat.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run(){
try {
tomcat.stop();
} catch (LifecycleException e) {
LOG.error("stoptomcat error.", e);
}
}
});
tomcat.getServer().await();
}
private static String getDefaultDocBase() {
File classpathDir = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile());
File projectDir =classpathDir.getParentFile().getParentFile();
return new File(projectDir,"src/main/webapp").getPath();
}
private static Tomcat createTomcat(int port,String contextPath, String docBase) throws Exception{
String tmpdir = System.getProperty("java.io.tmpdir");
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir(tmpdir);
tomcat.getHost().setAppBase(tmpdir);
tomcat.getHost().setAutoDeploy(false);
tomcat.getHost().setDeployOnStartup(false);
tomcat.getEngine().setBackgroundProcessorDelay(-1);
tomcat.setConnector(newNioConnector());
tomcat.getConnector().setPort(port);
tomcat.getService().addConnector(tomcat.getConnector());
Context context =tomcat.addWebapp(contextPath, docBase);
StandardServer server =(StandardServer) tomcat.getServer();
//APR library loader. Documentation at /docs/apr.html
server.addLifecycleListener(new AprLifecycleListener());
//Prevent memory leaks due to use of particularjava/javax APIs
server.addLifecycleListener(new JreMemoryLeakPreventionListener());
return tomcat;
}
private static Connector newNioConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol =(Http11NioProtocol) connector.getProtocolHandler();
return connector;
}
}

File not reading in selenium - maven framewaork

I tried to create a quick framework. in that I created below-mentioned classes:
Config file(All browsers path)
configDataProvider java class(reads the above file)
BrowserFactory class(has firefox browser object)
configDataProviderTest class(access data from dconfigDataProvider class)
now its not reading the paths mentioned in config.properties file.
I have provided all correct path and attached screenshots:
Looks like a problem is at your ConfigDataProvider class.
Firstly, you using Maven for building your project. Maven has defined project structure for code sources and for resources:
/src/main/java
/src/main/resorces
Thus, much better to put your .properties file there.
Second, you don't need to set the full path to your config file.
Relative path will be just enough. Something like below:
public class PropertiesFileHandler {
private static Logger log = Logger.getLogger(PropertiesFileHandler.class);
public static final String CONFIG_PROPERTIES = "src/main/resources/config.properties";
public static final String KEY = "browser.type";
public static BrowserType readBrowserType() {
BrowserType browserType = null;
Properties properties = new Properties();
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(CONFIG_PROPERTIES))) {
properties.load(inputStream);
browserType = Enum.valueOf(BrowserType.class, properties.getProperty(KEY));
} catch (FileNotFoundException e) {
log.error("Properties file wasn't found - " + e);
} catch (IOException e) {
log.error("Problem with reading properties file - " + e);
}
return browserType;
}
}
Lastly, if you are building framework you don't need to put everything under src/main/test. This path specifies tests with future possibilities to be executed with maven default lifecycle - mvn test.
The core of your framework can look like:
Two things which I noticed:
Don't give path in your properties path within ""
all the path seperators should be replaced with double backward slash \\ or single forward slash /

java.io.FileNotFoundException: resourceedge-config.xml (The system cannot find the file specified)

I was using jboss server to deploy my applications and it works fine. My jboss server is corrupted, and I'm changing to tomcat 7.0.55 and its giving me error on my console
"java.io.FileNotFoundException: resourceedge-config.xml (The system cannot find the file specified)"
The resourceedge-config.xml is readingfrom this method:
public static Properties loadSystemConfiguration() throws FileNotFoundException, IOException{
return loadSystemConfiguration(ConfigReader.getFile_Prefix() + "-config.xml");
}
And it's also calling from my application filter class too which is:
//get the application context
ServletContext context = filterConfig.getServletContext();
String configFilePrefix = context.getInitParameter(ApplicationFilter.APPLICATION_CONFIG_FILE_PREFIX);
if(configFilePrefix != null){
ConfigReader.setFile_Prefix(configFilePrefix);
}
try{
configuration = ConfigReader.loadSystemConfiguration();
}catch(Exception e){
e.printStackTrace();
configuration = null;
}
The resourceedge-config.xml is place inside C:\apache-tomcat-7.0.55\bin
Please I need help on this to enable to read the file resourceedge-config.xml.
Thanks

Schema "DB" Doesn't exist

I use the following code to create db connection
public final static String driver = "org.apache.derby.jdbc.ClientDriver";
public final static String connectionURL = "jdbc:derby:projectDB;create=true;user=user1;password=psssword";
public CreateConnectionDOA(String driver, String connectionURL) throws ClassNotFoundException,SQLException
{
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
conn.setAutoCommit(false);
}
The project was created in Netbeans-Platform-Application-Module.
When i run the project through netbeans platform 7.4.. it works properly.
But when i try to create a installer using netbeans and run.. the project opens but it also gives an exception
"ERROR 42Y07: Schema 'projectDB' does not exist
try fully pathing your DB in your url
public final static String connectionURL =
"jdbc:derby:d:/myproject/projectDB;create=true;user=user1;password=psssword";
Full path works because your relative path was probably wrong. With a correct relative path, it should work.
Keep in mind that current directory is your project directory; write the relative path (../dataBase if necessary works as expected) and it will work.

Mapr using Java

I am new to Hadoop, Mapr and Pivotal. I have written java code to write into pivotal but facing issue while writing into Mapr.
public class HadoopFileSystemManager {
private String url;
public void writeFile(String filePath,String data) throws IOException, URISyntaxException {
Path fPath = new Path(filePath);
String url = url = "hdfs://"+ip+":"+"8020";
FileSystem fs = FileSystem.get(new URI(url),new Configuration());
System.out.println(fs.getWorkingDirectory());
FSDataOutputStream writeStream = fs.create(fPath);
writeStream.writeChars(data);
writeStream.close();
}
}
This code works fine with pivoatal but fails with Mapr.
For Mapr i am using port = 7222.
I am getting the following error
"An existing connection was forcibly closed by the remote host"
Please let me know if am using the right port or anything needs to be changed in the code specific to Mapr.
I have stopped the iptables.
Any info is much appreciated.
Thanks
Heading
Try this code. But make sure you have MapR client setup in the node from where you are executing the test.
public class HadoopFileSystemManager {
private String url;
public void writeFile(String filePath,String data) throws IOException, URISyntaxException {
System.setProperty( "java.library.path", "/opt/mapr/lib" );
Path fPath = new Path(filePath);
String url = url = "hdfs://"+ip+":"+"8020";
FileSystem fs = FileSystem.get(new URI(url),new Configuration());
System.out.println(fs.getWorkingDirectory());
FSDataOutputStream writeStream = fs.create(fPath);
writeStream.writeChars(data);
writeStream.close();
}
}
Add the following to the classpath:
/opt/mapr/hadoop/hadoop-0.20.2/conf:/opt/mapr/hadoop/hadoop-0.20.2/lib/hadoop-0.20.2-dev-core.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/maprfs-0.1.jar:.:/opt/mapr/hadoop/hadoop-0.20.2/lib/commons-logging-1.0.4.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/zookeeper-3.3.2.jar
This statement in the above code: System.setProperty( "java.library.path", "/opt/mapr/lib" ); can be removed and can be supplied using -Djava.library.path too, if you are running your program from terminal when building.
/opt/mapr may not be your path to mapr files. If that's the case replace the path accordingly wherever applicable.
After comment:
If you are using Maven to build your project, try using the following in the pom.xml,
and with scope provided. MapR is compatible with the normal Apache Hadoop distribution too. So, while building you can use the same. Then when you run your program, you would supply the mapR jars in the classpath.
<dependency>
<groupid>hadoop</groupid>
<artifactid>hadoop</artifactid>
<version>0.20.2</version>
<scope>provided</scope>
</dependency>

Categories