我照着《JAVA2认证考试指南》上面的第26章写了程序,可是在运行数据库的接口的时候,提示说有很多的错误,能帮我看看么。简单说明一下,DBInterface 是Remote的一个子类,数据库接口的抽象类,DBServer 是数据库接口的一个实际类,代码见后面DB是后台的数据库DB, 代码如下,现在遇到的问题解释,在启动了DBServer后,调出了一堆一堆的错误,如下///////////////////////////////////////////////////////////////////////////////////////
DVServer err: access denied (java.io.FilePermission test.db read)
java.security.AccessControlException: access denied (java.io.FilePermission test.db read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at Server.DB.<init>(DB.java:25)
at Server.DBServer.<init>(DBServer.java:30)
at Server.DBServer.main(DBServer.java:67)
/////////////////////////////////////////////////////////////////////////////////////// import java.io.EOFException;
import java.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;public interface DBInterface extends Remote {
public void close () throws RemoteException;
public void rewind () throws RemoteException, IOException;
public boolean moreRecords () throws RemoteException, IOException;
public EmployeeRecord readRecord() throws RemoteException, IOException, EOFException;
public void writeRecord( EmployeeRecord er ) throws RemoteException, IOException;
}package Server;import java.io.*;
import java.rmi.*;
import java.rmi.server.*;/**
* @author n7063318
*
*/public class DBServer extends UnicastRemoteObject
implements DBInterface
{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String FILE_NAME = "test.db";
private static Object grant;
private DB db;
public DBServer() throws RemoteException, IOException {
// TODO Auto-generated constructor stub
this.db = new DB( this.FILE_NAME );
} @Override
public synchronized void close() throws RemoteException {
// TODO Auto-generated method stub
this.db.close();
}
@Override
public synchronized boolean moreRecords() throws RemoteException, IOException {
// TODO Auto-generated method stub
return db.moreRecords();
}
@Override
public synchronized EmployeeRecord readRecord() throws RemoteException, IOException,
EOFException {
// TODO Auto-generated method stub
return db.readRecord();
}
@Override
public synchronized void rewind() throws RemoteException, IOException {
// TODO Auto-generated method stub
db.rewind();
}
@Override
public synchronized void writeRecord(EmployeeRecord er) throws RemoteException,
IOException {
// TODO Auto-generated method stub
db.writeRecord( er );
}
public static void main( String[] args ) {
System.setSecurityManager( new RMISecurityManager() ); try{
DBServer dbServer = new DBServer();
Naming.rebind("DBServer", dbServer);
System.out.println("DBserver bound in registry");
} catch (Exception e) {
System.out.println( "DVServer err: " + e.getMessage() );
e.printStackTrace();
}
}
}
package Server;import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;/**
* @author n7063318
*
*/
public class DB { private RandomAccessFile file;
private boolean open;
public DB(String fileName ) throws IOException {
try {
open = true;
file = new RandomAccessFile( fileName , "rw" );
} catch (IOException x) {
close();
throw x;
}
}
public void close() {
if ( open ) {
try {
file.close();
} catch ( IOException x ) {
System.out.println(x.getMessage());
} finally {
open = false;
}
}
}
public void finalize() throws Throwable {
close();
super.finalize();
}
public void rewind() throws IOException {
file.seek(0);
}
public boolean moreRecords() throws IOException {
return (file.getFilePointer() < file.length() );
}
public EmployeeRecord readRecord() throws IOException, EOFException {
String _ssn = file.readUTF();
String _name = file.readUTF();
return new EmployeeRecord( _ssn, _name );
}
public void writeRecord( EmployeeRecord record ) throws IOException {
file.seek( file.length() );
file.writeUTF( record.getSSN() );
file.writeUTF( record.getNAME() );
}
}
DVServer err: access denied (java.io.FilePermission test.db read)
java.security.AccessControlException: access denied (java.io.FilePermission test.db read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at Server.DB.<init>(DB.java:25)
at Server.DBServer.<init>(DBServer.java:30)
at Server.DBServer.main(DBServer.java:67)
/////////////////////////////////////////////////////////////////////////////////////// import java.io.EOFException;
import java.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;public interface DBInterface extends Remote {
public void close () throws RemoteException;
public void rewind () throws RemoteException, IOException;
public boolean moreRecords () throws RemoteException, IOException;
public EmployeeRecord readRecord() throws RemoteException, IOException, EOFException;
public void writeRecord( EmployeeRecord er ) throws RemoteException, IOException;
}package Server;import java.io.*;
import java.rmi.*;
import java.rmi.server.*;/**
* @author n7063318
*
*/public class DBServer extends UnicastRemoteObject
implements DBInterface
{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String FILE_NAME = "test.db";
private static Object grant;
private DB db;
public DBServer() throws RemoteException, IOException {
// TODO Auto-generated constructor stub
this.db = new DB( this.FILE_NAME );
} @Override
public synchronized void close() throws RemoteException {
// TODO Auto-generated method stub
this.db.close();
}
@Override
public synchronized boolean moreRecords() throws RemoteException, IOException {
// TODO Auto-generated method stub
return db.moreRecords();
}
@Override
public synchronized EmployeeRecord readRecord() throws RemoteException, IOException,
EOFException {
// TODO Auto-generated method stub
return db.readRecord();
}
@Override
public synchronized void rewind() throws RemoteException, IOException {
// TODO Auto-generated method stub
db.rewind();
}
@Override
public synchronized void writeRecord(EmployeeRecord er) throws RemoteException,
IOException {
// TODO Auto-generated method stub
db.writeRecord( er );
}
public static void main( String[] args ) {
System.setSecurityManager( new RMISecurityManager() ); try{
DBServer dbServer = new DBServer();
Naming.rebind("DBServer", dbServer);
System.out.println("DBserver bound in registry");
} catch (Exception e) {
System.out.println( "DVServer err: " + e.getMessage() );
e.printStackTrace();
}
}
}
package Server;import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;/**
* @author n7063318
*
*/
public class DB { private RandomAccessFile file;
private boolean open;
public DB(String fileName ) throws IOException {
try {
open = true;
file = new RandomAccessFile( fileName , "rw" );
} catch (IOException x) {
close();
throw x;
}
}
public void close() {
if ( open ) {
try {
file.close();
} catch ( IOException x ) {
System.out.println(x.getMessage());
} finally {
open = false;
}
}
}
public void finalize() throws Throwable {
close();
super.finalize();
}
public void rewind() throws IOException {
file.seek(0);
}
public boolean moreRecords() throws IOException {
return (file.getFilePointer() < file.length() );
}
public EmployeeRecord readRecord() throws IOException, EOFException {
String _ssn = file.readUTF();
String _name = file.readUTF();
return new EmployeeRecord( _ssn, _name );
}
public void writeRecord( EmployeeRecord record ) throws IOException {
file.seek( file.length() );
file.writeUTF( record.getSSN() );
file.writeUTF( record.getNAME() );
}
}
解决方案 »
- 线程互斥----有点不懂,请教。。。
- 请教一下,为什么我无法叫醒wait的线程?
- JFrame作为一个子窗口,如果始终挡住父窗口
- 字符串数组分割,三处需要表示符是什么意思?代码哪里错了?
- 转义符的困惑-怎么就是转义不了呢,十分困惑。
- 新手,刚刚开始学JC,这是老师要我做的题,但我得的结果好郁闷~~~~
- 关于类路径
- 请教高手,怎么用icon 来显示程序在使用中和是空闲
- 有那位朋友看过Java2核心技术中的自定义布局管理器,在其中方法preferredLayoutSize的实现是怎样的?看不明白,高手解释一下
- 线程同步问题高手进
- TIJ4 Chapter Control, Exercise 9, page 153
- 做OA时,公文是保存进数据库里好,还是以文件形式保存在硬盘上好?
如果是unix/linux操作系统,看一下文件的权限设置
就是运用的RMI技术,可是不知道怎么加啊,书上有句话grant
{
// Allow everything for now
permission java.security.AllPermission;
};然后又说了在什么 D:\rmi 下面建议个什么东西,请问应该怎么做呢,我在 Eclipse 下作的, 客户端和服务端在两个不通的文件包下, client 与 sever 中。