JAR

2011-01-21 22 views
14

से DLL निकालें और लोड करें मेरा जावा एप्लिकेशन एक DLL लाइब्रेरी का उपयोग करता है। मैं इसे JAR फ़ाइल से कैसे प्राप्त कर सकता हूं?JAR

डीएलएल परियोजना के स्रोत फ़ोल्डर में है। मुझे इसे अपने जार में शामिल करना है, इसे रनटाइम पर निकालें (जार की एक ही निर्देशिका में) और इसे लोड करें।

उत्तर

25

आप अपनी लाइब्रेरी पथ (सिफारिश) इससे पहले कि आप इसे लोड करने की कोशिश में dll डाल करने के लिए की जरूरत है। ताकि आपको इसे जार से निकालना होगा और इसे lib पथ में कॉपी करना होगा।

private static void loadLib(String path, String name) { 
    name = System.mapLibraryName(name); // extends name with .dll, .so or .dylib 
    try { 
     InputStream in = ACWrapper.class.getResourceAsStream("/"+path + name); 
     File fileOut = new File("your lib path"); 
     OutputStream out = FileUtils.openOutputStream(fileOut); 
     IOUtils.copy(in, out); 
     in.close(); 
     out.close(); 
     System.load(fileOut.toString());//loading goes here 
    } catch (Exception e) { 
       //handle 
    } 
} 

नोट: // findjar:ACWrapper वर्ग स्थिर विधि पकड़े

+0

संकलक नहीं मिल सकता है FileUtils और IOUtiles – Oneiros

+0

आप जोड़ना [कॉमन्स-कब] (http जरूरत है। com/jar/commons-io/commons-io/1.4/commons-io-1.4.jar.html) अपने क्लासपाथ –

+0

में जार ठीक है लेकिन मैं जावा lib पथ कैसे प्राप्त कर सकता हूं? – Oneiros

-6
try { 
    InputStream in = Main.class.getResourceAsStream("/example-input.dll"); 

    File fileOut = new File("./example-output.dll"); 

    DataOutputStream writer = new DataOutputStream(new FileOutputStream(fileOut)); 

    long oneChar = 0; 
    while((oneChar = in.read()) != -1){ 
    writer.write((int)oneChar); 
    } 

    in.close(); 
    writer.close(); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 
+2

डेटाऑटपुटस्ट्रीम पूरी तरह से गलत है। – bmargulies

संबंधित मुद्दे