powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / ClassLoader
2 сообщений из 2, страница 1 из 1
ClassLoader
    #34223678
Wind2006
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Всем привет. Возник вопрос.
Класс загружается через сокет, подскажите как его можно слхранить в класс файл.

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
 package  loader;

 import  java.awt.Dimension;
 import  java.io.*;
 import  java.lang.reflect.Method;
 import  java.net.Socket;
 import  java.security.*;
 import  javax.swing.*;

 public   class  Loader  extends  SecureClassLoader {

    Socket s;
    DataOutputStream os;
    DataInputStream is;

     protected  Loader() {
    }

     protected  Loader(ClassLoader parent) {
         super (parent);
    }

     void  init()  throws  Exception {
        s =  new  Socket("host", port);
        os =  new  DataOutputStream(s.getOutputStream());
        is =  new  DataInputStream(s.getInputStream());
    }

     public   Class  loadClass(String str)  throws  ClassNotFoundException
    {
         Class  retValue =  null ;
         try  {
            retValue =  super .loadClass(str);
        }
         catch (ClassNotFoundException e) {
             try  {
                os.writeUTF(str);
                os.flush();
                 int  l = is.readInt();
                 if ( 0  == l) {
                    retValue =  null ;
                }  else  {
                     byte  b[] =  new   byte [l];
                     int  n;
                     for ( n =  0 ; n < b.length; n += is.read(b, n, b.length - n));
                    retValue = defineClass(str, b,  0 , b.length, getClass().getProtectionDomain().getCodeSource());
                    
File file =  new  File("C:/file.class");
OutputStream out =  new  FileOutputStream(file);  
 int  cnt;
out.write(b, 0 ,b.length);  //в данном месте не сохраняет правильно файл
out.close();
                }
            }
             catch (Exception e2) {
                StringWriter sw =  new  StringWriter();
                e2.printStackTrace( new  PrintWriter(sw));
                JTextArea msgTA =  new  JTextArea(sw.getBuffer().toString());
                JScrollPane msgScrollPane =  new  JScrollPane();
                msgScrollPane.setPreferredSize( new  Dimension( 600 ,  200 ));
                msgScrollPane.setViewportView(msgTA);
                JOptionPane.showMessageDialog( null , msgScrollPane, "\u041Eшиб\u043Aа",  0 );
                 throw  e;
            }
        }
       
           
         return  retValue;
    }

     protected  PermissionCollection getPermissions(CodeSource codesource) {
        Permissions permissions =  new  Permissions();
        permissions.add( new  AllPermission());
         return  permissions;
    }

     public   static   void  main(String args[]) {
         try  {
            Loader loader =  new  Loader(Thread.currentThread().getContextClassLoader());
            loader.init();
             Class  c = loader.loadClass("country.modules.module");
            Method m = c.getMethod("main",  new   Class [] {});
            
            m.invoke(c,  new  Object[] {
                 new  String[ 0 ]
            });
        }
         catch (Throwable e) {
            StringWriter sw =  new  StringWriter();
            e.printStackTrace( new  PrintWriter(sw));
            JTextArea msgTA =  new  JTextArea(sw.getBuffer().toString());
            JScrollPane msgScrollPane =  new  JScrollPane();
            msgScrollPane.setPreferredSize( new  Dimension( 600 ,  200 ));
            msgScrollPane.setViewportView(msgTA);
            JOptionPane.showMessageDialog( null , msgScrollPane, "\u041Eшиб\u043Aа",  0 );
            System.exit( 1 );
        }
    }
}

...
Рейтинг: 0 / 0
ClassLoader
    #34224077
LINUXER
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Wind2006Всем привет. Возник вопрос.
Класс загружается через сокет, подскажите как его можно слхранить в класс файл.

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
                    
File file =  new  File("C:/file.class");
OutputStream out =  new  FileOutputStream(file);  
 int  cnt;
out.write(b, 0 ,b.length);  //в данном месте не сохраняет правильно файл
out.close();


А что сохраняет? и почему это неправильно
!!!Имя класса должно соответствовать имени файла.
У вас файл вообще с маленькой буквы
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / ClassLoader
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]