2013-01-03 4 views
2

현재 Java Swing 응용 프로그램에 브라우저를 설치하기 위해 DJProject과 협력 중입니다. DJProject는 SWT를 사용하여 실행하며 SWT에 대한 경험이 거의 없습니다.Windows/Mac 및 32bit/64bit에서 SWT 지원

32 비트 및 64 비트 Windows 및 Mac을 모두 지원하고 싶습니다. 각 플랫폼에 swt.jar 파일이 있다는 것을 알고 있습니다. 모든 4 개의 swt.jar 라이브러리를 기본 응용 프로그램의 라이브러리로 클래스 경로에 추가했습니다. 나는의 적절한 변화를로드 자동으로 런타임에 자바를 말할 대해 갈 것입니다 방법

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM 

: 나는 오류를 얻을 예를 들어 Mac에서 응용 프로그램을 실행하려고하면

내 문제입니다 SWT 라이브러리.

+0

가능한 중복 [만들기 크로스 플랫폼 Java SWT 응용 프로그램] (http://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application) –

답변

3

:의

private void loadSwtJar() { 
    try { 
     Class.forName (ORG_ECLIPSE_SWT_WIDGETS_SHELL); 
     return; 
    } catch (ClassNotFoundException e) { 
     System.out.println (" ! Need to add the proper swt jar: "+e.getMessage()); 
    } 

    String osName = System.getProperty("os.name").toLowerCase(); 
    String osArch = System.getProperty("os.arch").toLowerCase(); 

    //NOTE - I have not added the mac and *nix swt jars. 
    String osPart = 
     osName.contains("win") ? "win" : 
     osName.contains("mac") ? "cocoa" : 
     osName.contains("linux") || osName.contains("nix") ? "gtk" : 
     null; 

    if (null == osPart) 
     throw new RuntimeException ("Cannot determine correct swt jar from os.name [" + osName + "] and os.arch [" + osArch + "]"); 

    String archPart = osArch.contains ("64") ? "64" : "32"; 

    System.out.println ("Architecture and OS == "+archPart+"bit "+osPart); 

    String swtFileName = "swt_" +osPart + archPart +".jar"; 
    String workingDir = System.getProperty("user.dir"); 
    String libDir = "\\lib\\"; 
    File file = new File(workingDir.concat(libDir), swtFileName); 
    if (!file.exists()) 
     System.out.println("Can't locate SWT Jar " + file.getAbsolutePath()); 

    try { 
     URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader(); 
     Method addUrlMethod = URLClassLoader.class.getDeclaredMethod ("addURL", URL.class); 
     addUrlMethod.setAccessible (true); 

     URL swtFileUrl = file.toURI().toURL(); 
     //System.out.println("Adding to classpath: " + swtFileUrl); 
     addUrlMethod.invoke (classLoader, swtFileUrl); 
    } 
    catch (Exception e) { 
     throw new RuntimeException ("Unable to add the swt jar to the class path: " + file.getAbsoluteFile(), e); 
    } 
} 
1

How would I go about to automatically tell Java at run-time to load the proper variation of the SWT library?

그렇지 않습니다. 컴퓨터 (Windows 및 Mac)와 운영 체제 (32 비트 및 64 비트) 각각에 대해 4 개의 jar 파일을 만듭니다.

각 jar 파일이 하나의 시스템과 하나 명의 운영 체제에 적합한 SWT 항아리 라이브러리를 포함

당신은 OS 및 Java 버전을 감지하고 동적으로 적절한 항아리로드 할 수 있습니다
+0

그게 전부 4 병 파일을 가지고 있지만 자바 잘못로드하는 것, 나는 4를 모두 가지고 있는데, 그것은 Win 64에서 작동하지만, Mac 64에서 테스트 할 때 작동하지 않는다면, 나는 그 에러를 얻는다. –

+0

아마도 나는 명확하지 않습니다. Mac 64 비트 병은 Mac 64 비트 시스템에서만 실행됩니다. 다른 3 개의 병은 Mac 64 비트 시스템과 가까이 있지 않습니다. 다른 3 개의 병에 대해서도 마찬가지입니다. –

+0

나는 이해한다. 그러나 나는 나의 응용의 4 개의 다른 비뚤어 짐을 만들고 싶지 않다. 한 번에 4 개의 플랫폼에서 작동하는 하나의 앱을 출시하고 싶습니다. 그래서 내가 묻는 것은 런타임에 앱이 실행되는 플랫폼에 따라 사용할 swt 항아리를 정확히 선택할 수있을 것입니다. –