views:

265

answers:

3

Hi, What effect will Java RMI have on the following line of code?

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Its not working for me in an RMI call. I am new to java and RMI so please elaborate your answer in detail.

Edit:

//String connect to SQL server
String url = "jdbc:sqlserver://" + strServerIPAddress + ":1433" + ";DatabaseName=" + strDatabaseName;

try {

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

} 
catch (java.lang.ClassNotFoundException e) 

{ 
    System.out.println(e.getCause().toString());
    return false;
}

I have pasted the code. Can you please tell how to get the full stack trace?

Here is the batch file that I use to run the my code

% I am in my source code dir

javac -d classes -cp classes *.java
rmic -classpath classes -d classes myrmi.DummyImpl myrmi.BookImpl    
start rmiregistry -J-Dsun.rmi.loader.logLevel=VERBOSE

start java -cp classes -Djava.rmi.server.codebase=file:///C:\NetBeansProjects\MyProject\src\myrmi\classes\ -Djava.security.policy=java.policy 

myrmi.Server SEB 
start java -cp classes -Dsun.rmi.loader.logLevel=VERBOSE  -Djava.security.policy=java.policy myrmi.Client
A: 

When using Class.forName() the string passed to the method must contain a class that is present in your classpath.

Mr. Will
To make my life easier for moment I have put all my classes including these jdbc driver jar files in 'classes' directory. And I am already specifying this directory in my classpath i.e. start java -cp classes .The same code works fine when call the code in a simple java app i.e. without RMI
Mike
Adding this comment for the sake of other who may come here and face the same problem.Without the code was working because instead of using the batch file I was running the code using netbeans and I already added the jar file to the list of external packages.
Mike
+2  A: 

Is the JAR file with the com.microsoft.sqlserver.jdbc.SQLServerDriver available on the classpath of the VM where the remote instance in running?

-cp classes will only grab the .class files under there, so if the SQLDriver is in a JAR or ZIP file (which is where it likely is) then it will not be found. You would need to do "-cp classes;< path to driver >"

TofuBeer
I have only one directory where all my class files are located. And i pasted this jdbc jar file in the same directory. And I specifying teh name of the directory in classpath using "start java -cp classes " for both server and client
Mike
@mike Did you paste the jar, or the **contents** of the jar? That will make a difference. If you pasted the jar itself then your classpath won't find it. You would need to add the jar to your cp as well e.g -cp classes;classes/jdbc.jar
Michael Rutherfurd
:( I changed the value for -cp argument to '-cp classes;classes/sqljdbc4.jar' but the result is still the same
Mike
ooops !! I am sorry. I didn't check the output correctly. Changing class path to 'classes;classes/sqljdbc4.jar' worked for me.Thanks every one.
Mike
A: 

The probable cause is that your don't have permissions to either load the driver or connect to the remote database. Roughly speaking, all the frames on the stack and for the point the current thread was created must have the required permissions (java.security.AccessController.doPrivileged can be used to play about with this). There are some debug option that can be set through system properties for showing security checks (see the source in or around AccessController).

I am unsure as to where you are trying to make the connection. If it is from code dynamically loaded by RMI, that's a weird thing to do.

You can get a full stack trace from an exception with exc.printStackTrace().

Tom Hawtin - tackline