Class.forName 在 Java RMI 调用中不起作用
Java RMI 对以下代码行有何影响?
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
它在 RMI 调用中对我不起作用。我对 java 和 RMI 很陌生,所以请详细说明您的答案。
编辑:
//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;
}
我已经粘贴了代码。您能告诉我如何获取完整的堆栈跟踪吗?
这是我用来运行我的代码的批处理文件
%我在我的源代码目录中
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
包含 com.microsoft.sqlserver.jdbc.SQLServerDriver 的 JAR 文件在运行远程实例的 VM 的类路径上是否可用?
-cp 类只会获取那里的 .class 文件,因此如果 SQLDriver 位于 JAR 或 ZIP 文件中(很可能位于该文件中),则不会找到它。您需要执行“-cp classes;<驱动程序路径>”
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 >"
使用 Class.forName() 时,传递给该方法的字符串必须包含类路径中存在的类。
When using Class.forName() the string passed to the method must contain a class that is present in your classpath.
可能的原因是您没有加载驱动程序或连接到远程数据库的权限。粗略地说,堆栈上的所有帧以及创建当前线程的点都必须具有所需的权限(可以使用 java.security.AccessController.doPrivileged 来解决这个问题)。可以通过系统属性设置一些调试选项以显示安全检查(请参阅
AccessController
中或周围的源代码)。我不确定你想在哪里建立联系。如果它来自 RMI 动态加载的代码,那就很奇怪了。
您可以使用
exc.printStackTrace()
从异常中获取完整的堆栈跟踪。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 aroundAccessController
).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()
.