我正面臨一個簡單的問題並尋求幫助。javax.naming.NoInitialContextException:無法實例化類:orj.jboss.naming.remote.client.InitialContextFactory
這裏的異常消息:
javax.naming.NoInitialContextException: Cannot instantiate class: orj.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: orj.jboss.naming.remote.client.InitialContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at ReceveurJMS.<init>(ReceveurJMS.java:44)
at ReceveurJMS.main(ReceveurJMS.java:91)
Caused by: java.lang.ClassNotFoundException: orj.jboss.naming.remote.client.InitialContextFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 6 more
這裏是我的類PanelPhoto
:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class PanelPhoto extends JPanel{
private BufferedImage bufferedImage;
public void paint(Graphics g){
g.drawImage(bufferedImage, 0, 0, this.getWidth(),this.getHeight(),null);
}
public BufferedImage getBufferedImage() {
return bufferedImage;
}
public void setBufferedImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
}
這裏是我的類EnvoyeurPhoto
:
public class EnvoyeurPhoto extends JFrame {
private JLabel jLabelPhoto = new JLabel("Photo");
private JComboBox<String> jComboBoxPhotos;
private JButton jButtonEnvoyer = new JButton("Envoyer");
private PanelPhoto panelPhoto = new PanelPhoto();
private Connection conn;
private Destination destination;
public EnvoyeurPhoto() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel jPanelN = new JPanel();
File f = new File("photos");
String[] photos = f.list();
jComboBoxPhotos = new JComboBox<String>(photos);
jPanelN.setLayout(new FlowLayout());
jPanelN.add(jLabelPhoto);
jPanelN.add(jComboBoxPhotos);
jPanelN.add(jButtonEnvoyer);
this.add(jPanelN, BorderLayout.NORTH);
this.add(panelPhoto, BorderLayout.CENTER);
this.setBounds(10, 10, 400, 300);
this.setVisible(true);
init();
jComboBoxPhotos.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String photo = (String) jComboBoxPhotos.getSelectedItem();
File file = new File("photos/" + photo);
BufferedImage bi = ImageIO.read(file);
panelPhoto.setBufferedImage(bi);
panelPhoto.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
jButtonEnvoyer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
Session session = conn.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
File f = new File("photos/" + (String) jComboBoxPhotos.getSelectedItem());
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[(int) f.length()];
fis.read(data);
StreamMessage message = session.createStreamMessage();
message.writeString((String) jComboBoxPhotos.getSelectedItem());
message.writeInt(data.length);
message.writeBytes(data);
producer.send(message);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// RQ:si on a pas faitlorsqu'on a crée la session true c-a-d
// on a transactionnel(false: n est pas transactionnel) dans
// cette il faut faire session.commit();
// session.commit();
// conn.close();
}
});
}
public void init() {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "orj.jboss.naming.remote.client.InitialContextFactory");
p.put(Context.PROVIDER_URL, "remote://localhost:4447");
p.put(Context.SECURITY_PRINCIPAL, "sid");
p.put(Context.SECURITY_CREDENTIALS, "azerty");
try {
Context ctx;
ctx = new InitialContext(p);
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
conn = factory.createConnection("sid", "azerty");
destination = (Destination) ctx.lookup("jms/queue/test");
conn.start();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new EnvoyeurPhoto();
}
}
這裏是我的類ReceveurJMS
:
public class ReceveurJMS extends JFrame {
private PanelPhoto jPanelPhoto = new PanelPhoto();
public ReceveurJMS() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(jPanelPhoto, BorderLayout.CENTER);
this.setBounds(10, 10, 400, 300);
this.setVisible(true);
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "orj.jboss.naming.remote.client.InitialContextFactory");
// p.put(Context.PROVIDER_URL,"remote://localhost:8080");
p.put(Context.PROVIDER_URL, "remote://localhost:4447");
p.put(Context.SECURITY_PRINCIPAL, "sid");
p.put(Context.SECURITY_CREDENTIALS, "azerty");
try {
Context ctx;
ctx= new InitialContext(p);
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
Connection conn = factory.createConnection("sid", "azerty");
Destination destination = (Destination) ctx.lookup("jms/queue/test");
Session session = conn.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
//conn.start();
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
StreamMessage m = (StreamMessage) message;
String nomPhoto = m.readString();
int length = m.readInt();
byte[] data = new byte[length];
m.readBytes(data);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
BufferedImage bi = ImageIO.read(bais);
jPanelPhoto.setBufferedImage(bi);
jPanelPhoto.repaint();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
conn.start();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new ReceveurJMS();
}
}
您需要將一些jboss客戶端jar添加到您的應用程序路徑。這些罐子取決於你的jboss版本。請參閱jboss文檔。 –
我已經添加了jboss-client.jar – khalid
你的版本是什麼?有更多的罐子添加 –