想做一个串口通信的程序,可是在安装comm时出错了import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.comm.*;//包含comm类包,才能使用其API函数
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] arg){
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while(en.hasMoreElements())
{
portId = (CommPortIdentifier)en.nextElement();
/*如果端口类型是串口,则打印出其端口信息*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portId.getName());
}
}
}}
错误是:java.io.IOException: Error instantiating class com.sun.comm.Win32Driver
com.sun.comm.Win32Driver
at javax.comm.CommPortIdentifier.loadDriver(CommPortIdentifier.java:239) at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:109)
at Test.main(Test.java:11)
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.comm.SunrayIn
fo.isSessionActive()Z
at com.sun.comm.SunrayInfo.isSessionActive(Native Method)
at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155)
at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100)
at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138)
at Test.main(Test.java:11)
import java.awt.event.*;
import javax.swing.*;
import javax.comm.*;//包含comm类包,才能使用其API函数
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] arg){
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while(en.hasMoreElements())
{
portId = (CommPortIdentifier)en.nextElement();
/*如果端口类型是串口,则打印出其端口信息*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portId.getName());
}
}
}}
错误是:java.io.IOException: Error instantiating class com.sun.comm.Win32Driver
com.sun.comm.Win32Driver
at javax.comm.CommPortIdentifier.loadDriver(CommPortIdentifier.java:239) at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:109)
at Test.main(Test.java:11)
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.comm.SunrayIn
fo.isSessionActive()Z
at com.sun.comm.SunrayInfo.isSessionActive(Native Method)
at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155)
at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100)
at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138)
at Test.main(Test.java:11)
1.COMM.JAR;
2.javax.comm.properties;
3.win32com.dll;
然后把这三个文件放到如下目录中:
其中:JDKDIR是JDK的安装目录,JREDIR是JRE的安装目录。
假如你把JAVA开发包装在C:\Program Files\Java中,里面有两个文件夹,一个是JDK,一个是JRE。
COMM.JAR放到JDKDIR/jre/lib/ext;
javax.comm.properties放到JDKDIR/jre/lib。
win32com.dll放到JDKDIR/bin中,
如果用ECLIPSE可能需要你手动导入comm.JAR,相信这个大家都会。
配置到这里就完成了吗?
没有,有的机器会报错,如果说没有win32com.dll,那么请把win32com.dll放到JreDIR/bin中就可以了。
如果发现获得的端口列表中为空,那是因为机器没有找到javax.comm.properties文件,同样把javax.comm.properties放到JreDIR/jre/lib中就可以了,具体是放到JDK还是JRE中,我也不太明白。估计和机器和JDK版本有关系了,不过,肯定有效的方法是你把JDK还是JRE,全都放了.
大家可以总结一下。