I tried a java program which needs a jar file in another path in a unix server.
i want this jar to be used when ever i need the program as i use a servelet to call this program.
When i compile the program including jar like java -cp .;*.jar myProgram , it works.
But when i call the below function from a servlet , the jar is not getting inclued and so get error.
please advise.
Its the same when i try from my eclipse project where i had the jar included.
void doBless(int i,String Envs) {
String url = "jdbc:sybase:Tds:ramu-sys:5000/".concat(Envs);
connection b=new connection();
System.out.println(url);
String queryString;
String temp;
try {
Class.forName("com.sybase.jdbcx.SybDriver");
}
catch( Exception e ) {
System.out.println("Failed to load sybase driver.");
return;
}
try {
ResultSet myResultSet =null;
Connection con = DriverManager.getConnection(url, "sa", "");
Statement select = con.createStatement();
temp="insert into Persons (P_Id,FirstName,LastName,Address,City) values (" +i+",'" + blessArray.get(i)+ "','"+ blessArray.get(i)+ "','"+ blessArray.get(i)+ "','"+ blessArray.get(i)+ "')";
System.out.println(temp);
select.executeQuery(temp);
select.close();
con.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
I think there is a misunderstanding of some environment related concepts and its the reason of why you’re getting such an error.
When you’re running a ‘usual’ java progam as you stated with ‘java -cp’ you by yourself explicitly define which jars should be in the classpath.
This is not the case when you’re running the servlet. Servlets can’t by directly run by java (they don’t have method ‘main’ if you want).
Instead they’re designed to be run inside another java based application (called container). In terms of deployment, you compile your servlet into a class file, put it into the special directory and run your container.
Container application reads the files in the directory, recognizes the servlet and creates an instance of it. In other words, it manages the servlet for you.
Now, in order to recognize the classes in runtime, containers use their custom class loaders, so you have to deploy your jars that the server relies on somehow that these classloaders will correctly load the classes from these jars.
Suggested above tools like maven or ant – just handle the process of building your application and can ‘put’ your jar in a correct place, but IMO you should still understand what goes where and what are responsibilities of each component/tool.
Ok, So, what are these places where the jar should be stored.
There is more than one such a place, and you should understand what are the consequences of putting the jar in each place.
The most easy solution is to wrap your servlet in WAR file (or even leave it as a directory in a predefined layout).
WAR (Web Application Archive) is a file/directory with all the servlets and jars inside.
Since all the web containers should understand the WAR’s layout, you can place your JAR in WEB-INF/lib folder inside your WAR file. I believe you should go for this option.
Another option is to put the jar in a directory where the jars needed for the container itself reside. This is less preferable, but sometimes needed.
I hope this makes things clearer 🙂 Good Luck!