2015-02-01 3 views
2

내 프로그램은 RS232를 통해 통신해야하므로 .jar를 사용하고 두 개의 .dll은 RXTX을 사용합니다. 마지막에는 단일 .jar 파일에서 실행하려고합니다..dll을 사용하여 실행 가능한 jar 파일을 만드는 방법 - RXTX

이 문제를 해결하기 위해 this 튜토리얼을 사용했습니다. 여기

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path

내 코드

private static final String LIB = "lib/"; 
private final static String RXTXPARALLEL = "rxtxParallel"; 
private final static String RXTXSERIAL = "rxtxSerial"; 

static { 
    try { 
     System.loadLibrary(RXTXSERIAL); 
     System.loadLibrary(RXTXPARALLEL); 
     } catch (UnsatisfiedLinkError e) { 
      loadFromJar(); 
     } 
    } 

public static void main(String[] args) { 
    //RS232 is this class 
    RS232 main = new RS232(); 
    main.connect("COM15"); 

} 

private static void loadFromJar() { 
    String path = "AC_" + new Date().getTime(); 
    loadLib(path, RXTXPARALLEL); 
    loadLib(path, RXTXSERIAL); 
} 


private static void loadLib(String path, String name) { 
    name = name + ".dll"; 
    try { 
     InputStream in = ResourceLoader.load(LIB + name); 
     File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" 
      + path + LIB + name); 

     OutputStream out = FileUtils.openOutputStream(fileOut); 
     IOUtils.copy(in, out); 
     in.close(); 
     out.close(); 
     System.load(fileOut.getAbsolutePath()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void connect(String portName) { 

    CommPortIdentifier portIdentifier; 
    try { 
     //Here the exception is thrown 
     portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
    } catch (NoSuchPortException exc) { 
     exc.printStackTrace(); 
     return; 
    } 
    //... some other code 
} 

의 최소한의 예입니다 방법이 있나요 : 나는 이클립스에서 프로그램을 실행한다면 (또는 콘솔에서 보낸 후) 나는이 예외가 실행 가능한 .jar 파일을 얻으려면?

답변

1

몇 가지 옵션이 있습니다. .dll 파일을 런타임 폴더에 복사하고 프로그램 시작 시마다 파일을 무시하십시오. 두 번째 옵션은 파일을 수정 폴더에 복사하고 MS Windows의 환경 변수에 폴더의 경로를 추가하는 것입니다. 처음 시작할 때마다 파일을 덮어 쓸 수도 있습니다.

또 다른 가능성은 런타임시 MS Windows 환경 변수에 임시 폴더를 추가하는 것입니다. 그러나이 솔루션에주의하십시오. 자세한 내용은 this 게시물을 읽어보십시오.

static { 
    try { 
     System.loadLibrary(RXTXSERIAL); 
     System.loadLibrary(RXTXPARALLEL); 
    } catch (UnsatisfiedLinkError exc) { 
     initLibStructure(); 
    } 
} 


private static void initLibStructure() { 

    try { 
     //runntime Path 
     String runPath = new File(".").getCanonicalPath(); 

     //create folder 
     File dir = new File(runPath + "/" + LIB); 
     dir.mkdir(); 

     //get environment variables and add the path of the 'lib' folder 
     String currentLibPath = System.getProperty("java.library.path"); 
     System.setProperty("java.library.path", 
       currentLibPath + ";" + dir.getAbsolutePath()); 

     Field fieldSysPath = ClassLoader.class 
       .getDeclaredField("sys_paths"); 
     fieldSysPath.setAccessible(true); 
     fieldSysPath.set(null, null); 

     loadLib(runPath, RXTXPARALLEL); 
     loadLib(runPath, RXTXSERIAL); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private static void loadLib(String path, String name) { 
    name = name + ".dll"; 
    try { 
     InputStream in = ResourceLoader.load(LIB + name); 
     File fileOut = new File(path + "/" + LIB + name); 

     OutputStream out = FileUtils.openOutputStream(fileOut); 
     IOUtils.copy(in, out); 
     in.close(); 
     out.close(); 
     System.load(fileOut.getAbsolutePath()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

덕분에, 그 작품! – anmi