Results 1 to 7 of 7
  1. #1
    Moons is offline Member
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    4
    Rep Power
    0

    Question Problems with RMI and NetBeans

    Hi :)

    I'm trying to do an application RMI using NetBeans, but it doesn't work anyway.

    The class Imagem is the server, JanelaRMI is the client and IntImagem is the interface. Can you guys see what's the problem with them? Every time I fix something an new error appears (like "connection refused", "$Proxy0 cannot be cast", etc). I don't what else can I do :confused:

    class Imagem (server)
    Java Code:
    import java.io.*;
    import java.net.MalformedURLException;
    import java.rmi.AlreadyBoundException;
    import java.rmi.*;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
    import javax.swing.ImageIcon;
    
    public class Imagem extends UnicastRemoteObject implements IntImagem {
    
        public Imagem() throws RemoteException {}
    
        public ImageIcon converterImagem (File file) throws FileNotFoundException, IOException {
    
            String nome = file.getName();
            FileOutputStream imagem = new FileOutputStream(nome);
            byte[] b = new byte[(int)file.length()];
                imagem.flush();
                imagem.write(b);
                imagem.close();
            ImageIcon image = new ImageIcon(b);
    
            return image;
        }
    
        public static void main (String args[]) throws RemoteException, MalformedURLException, AlreadyBoundException {
            Imagem img = new Imagem();
            IntImagem stub = (IntImagem) UnicastRemoteObject.exportObject(img, 1099);
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("IntImagem", stub);
            
            }
    }

    class JanelaRMI (client) - this class is a JFrame.
    Java Code:
    (...)
    
    private void jAbrirActionPerformed(java.awt.event.ActionEvent evt) {                                       
            try {
    
                jImagem.setText("");
                            
                //Naming.lookup("rmi://127.0.0.1/MsgSrv");
                
    
                //inicio do FileChooser
                JFileChooser fc = new JFileChooser();
    
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int result = fc.showOpenDialog(this);
    
                if(result == JFileChooser.CANCEL_OPTION)
                  file = null;
                else
                  file = fc.getSelectedFile();
                //fim do FileChooser
                //Imagem img = (Imagem) Naming.lookup("rmi://localhost/MsgSrv");
                Registry registry = LocateRegistry.getRegistry(1099);
                IntImagem stub = (IntImagem) registry.lookup("IntImagem");
                ImageIcon imagem = stub.converterImagem(file);
                jImagem.setIcon(imagem);
    
            } catch (Exception ex) {
                jImagem.setText("Erro:" + ex.getMessage());
                }
    
        }
    (...)
    interface IntImagem:
    Java Code:
    import java.io.*;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import javax.swing.ImageIcon;
    
    public interface IntImagem extends Remote {
    
        public ImageIcon converterImagem(File file) throws RemoteException, FileNotFoundException, IOException;
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Moons is offline Member
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    4
    Rep Power
    0

    Default

    I just modificated the code one more time, and I think the connection's working:

    Imagem:
    Java Code:
    (...)
    public static void main (String args[]) throws RemoteException, MalformedURLException, AlreadyBoundException {
            Imagem img = new Imagem();
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.bind("Imagem", img);
    JanelaRMI:
    Java Code:
    (...)
    Registry registry = LocateRegistry.getRegistry("localhost", 1099);
                IntImagem remote = (IntImagem) registry.lookup("Imagem");
    The point of this application is to send and receive an image and show it in the JLabel. Now the problem probably is the method to send the array of bytes (that's the image) :/
    Method:
    Java Code:
    public ImageIcon converterImagem (File file) throws FileNotFoundException, IOException {
    
            String nome = file.getName();
            FileOutputStream imagem = new FileOutputStream(nome);
            byte[] b = new byte[(int)file.length()];
                imagem.flush();
                imagem.write(b);
                imagem.close();
            ImageIcon image = new ImageIcon(b);
    
            return image;
        }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    Moons is offline Member
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    4
    Rep Power
    0

    Default

    Nothing :(

    So I rewrite the classes and now they're like this:

    FileImpl (for the methods implementation)
    Java Code:
    public class FileImpl extends UnicastRemoteObject implements IntImagem {
    
        public String name;
    
        public FileImpl (String s) throws RemoteException {
            super();
            name = s;     }
    
        public byte[] downloadFile (String fileName) throws RemoteException, FileNotFoundException {
            try {
                File file = new File(fileName);
                byte buffer[] = new byte [(int)file.length()];
                BufferedInputStream input = new BufferedInputStream(new FileInputStream (fileName));
                input.read(buffer, 0, buffer.length);
                input.close();
                return buffer;
            } catch (Exception e) {
                    System.out.println(e.getMessage());
            return (null);
        }
    }
        public ImageIcon converterImagem (File file) throws RemoteException, FileNotFoundException, IOException {
    
            String nome = file.getName();
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file.getName()));
            output.flush();
            output.write(b);
            output.close();
            ImageIcon image = new ImageIcon(b);
    
            return image;
        }
    }
    Servidor (the server)
    Java Code:
    public class Servidor {
    
        public static void main(String argv[]) {
            if(System.getSecurityManager() == null) {
                System.setSecurityManager(new RMISecurityManager());
    
            } try {
                IntImagem fi = new FileImpl("fileserver");
                Naming.rebind("//127.0.0.1/FileServer", fi);
    
    } catch(Exception e) {
        System.out.println("FileServer: "+e.getMessage());
            }
                }
    }
    JanelaRMI (now that's the client)
    Java Code:
    (...)
        private void jAbrirActionPerformed(java.awt.event.ActionEvent evt) {
            try {
                jImagem.setText("");                  
                
                JFileChooser fc = new JFileChooser();
    
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int result = fc.showOpenDialog(this);
    
                if(result == JFileChooser.CANCEL_OPTION)
                  file = null;
                else
                  file = fc.getSelectedFile();
                
                if(System.getSecurityManager() == null) {
    
                System.setSecurityManager(new RMISecurityManager()); }
                      
                IntImagem fi = (IntImagem) Naming.lookup("//localhost/FileServer");
                FileImpl img = new FileImpl("fileserver");
                jImagem.setIcon(img.converterImagem(file));
    
            } catch (Exception ex) {
                jImagem.setText("Erro:" + ex.getMessage());
                }
    (...)
    And there's an interface called IntImagem declaring the methods converterImagem() and downloadFile().


    When I run the server nothing goes wrong but when I run the client (JanelaRMI) and choose the image file this error message appears:

    access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)

    So it's failing in the connection, before the method to show the image can execute :( I've searched some information about that error and apparently the problem's due to the security policy of the Java versions below Java 2, and I would have to create a new security policy file, but I have no idea how to do this :confused:

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Moons is offline Member
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the interest :)

    The connection's working now. Actually, the problem was solved with a simple rebind and lookup, like this:

    Java Code:
    //Server
    Registry registry = LocateRegistry.getRegistry();
    	registry.rebind("TestServer", this);
    
    //Client
    String url = "rmi://" + host + "/4Chat";
    	Server server = (Server)Naming.lookup(url);
    But I'm still having problems with the method to convert the image. I have the JFrame that opens the fileChooser, and one method to convert the image. When I run the JFrame everything goes right, there isn't any error message or something like that, but the image doesn't appear in the jLabel. Could you please help me to see what's the problem with this method? :confused:

    Java Code:
    public ImageIcon convertImage (String fileName) throws RemoteException, FileNotFoundException, IOException {
    
                File file = new File(fileName);
                InputStream input = new FileInputStream(file);
                OutputStream output = new ByteArrayOutputStream();
                byte[] buffer = new byte[(int)file.length()];
                int readBytes = -1;
                while ((readBytes = input.read(buffer, 0, buffer.length)) != -1) {
                output.write(buffer, 0, readBytes);
       }
      
       output.flush();
       output.close();
       input.close();
    
       buffer = ((ByteArrayOutputStream) output).toByteArray();
       ImageIcon image = new ImageIcon(buffer);
            return image;        
        }
    Last edited by Moons; 12-11-2010 at 03:22 AM.

Similar Threads

  1. problems with connecting to Oracle DB in NetBeans
    By m16k2002 in forum Web Frameworks
    Replies: 6
    Last Post: 03-25-2009, 10:14 AM
  2. Noob problems with netbeans
    By mushy in forum NetBeans
    Replies: 3
    Last Post: 01-28-2009, 03:30 AM
  3. Problems with NetBeans
    By gabriel in forum NetBeans
    Replies: 2
    Last Post: 11-07-2007, 09:49 PM
  4. Replies: 0
    Last Post: 08-07-2007, 07:01 AM
  5. Problems to install NetBeans
    By fred in forum NetBeans
    Replies: 1
    Last Post: 08-02-2007, 05:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •