我照着《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() );
}
}

解决方案 »

  1.   

    如果是windows操作系统,看一下是不是被别的程序或者应用打开了。
    如果是unix/linux操作系统,看一下文件的权限设置
      

  2.   

    用到rmi技术的话,需要在运行的时候加入policy
      

  3.   


    就是运用的RMI技术,可是不知道怎么加啊,书上有句话grant 
    {
    // Allow everything for now
    permission java.security.AllPermission;
    };然后又说了在什么 D:\rmi 下面建议个什么东西,请问应该怎么做呢,我在 Eclipse 下作的, 客户端和服务端在两个不通的文件包下, client 与 sever 中。