[Rxtx] LibExtPath to make it easier

Adrian Crum adrian.crum at yahoo.com
Tue Nov 16 09:03:28 MST 2010


Putting platform-specific code in Java defeats the "write once, run anywhere" intent of the language.

-Adrian

--- On Tue, 11/16/10, Diego Cirujano <dicir00 at gmail.com> wrote:

From: Diego Cirujano <dicir00 at gmail.com>
Subject: [Rxtx] LibExtPath to make it easier
To: rxtx at qbang.org
Date: Tuesday, November 16, 2010, 4:42 AM

Hi all,
I've been using this lib for a long time so now I would like to contribute in order to make it easier to use.
I programmed a class that gets the library(so, dll) for me. This is the code:



 /*    * LibExtPath.java  

  *     * Copyright (C) 2009 Diego Cirujano  

  *     * This program is distributed in the hope that it will be useful,  

  * but WITHOUT ANY WARRANTY; without even the implied warranty of    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  

  * GNU General Public License for more details.    *   

  * You should have received a copy of the GNU General Public License    * along with this program.  If not, see <http://www.gnu.org/licenses/>.  

  */  
package es.unizar.tecnodiscap.util;


/** *

 * @author ciru */import java.lang.reflect.*;

import java.util.logging.Level;import java.util.logging.Logger;


public class LibExtPath {


    public static void configureRXTX() {


        String pathname = "./libext/rxtx/";


        if (isWindows() && is32()) {            // windows 32

            pathname = pathname + "Windows/win32";        } else if (isWindows() && is64()) {

            // windows 64            pathname = pathname + "Windows/win64";

        } else if (isLinux() && is32()) {            // linux 32

            pathname = pathname + "Linux/i686-unknown-linux-gnu";        } else if (isLinux() && is64()) {

            // linux 64            pathname = pathname + "Linux/x86_64-unknown-linux-gnu";

        } else if(isLinux() && isIA()){            // linux ia

            pathname = pathname + "Linux/ia64-unkown-linux-gnu";        } else if (isMac()){

            // mac            pathname = pathname + "Mac_OS_X";

        } else {            String osname = System.getProperty("os.name", "").toLowerCase();

            String osarch = System.getProperty("os.arch", "").toLowerCase();            System.out.println("Sorry, your operating system is not supported: "

                    + "\n" + osname                    + "\n" + osarch);

            System.exit(-1);        }
        addDir(pathname);
    }
    public static boolean is32(){

        String osarch = System.getProperty("os.arch", "").toLowerCase();       // return true;

        return ((osarch.indexOf("i386")!=-1 || osarch.indexOf("i686")!=-1)||osarch.indexOf("x86")!=-1);


    }


    public static boolean is64(){        String osarch = System.getProperty("os.arch", "").toLowerCase();

        return osarch.indexOf("64")>0;    }


    public static boolean isIA(){
        String osarch = System.getProperty("os.arch", "").toLowerCase();
        return osarch.indexOf("ia")>0;    }


    public static boolean isWindows() {

        String os = System.getProperty("os.name").toLowerCase();        //windows

        return (os.indexOf("win") >= 0);    }


    public static boolean isMac() {

        String os = System.getProperty("os.name").toLowerCase();        //Mac

        return (os.indexOf("mac") >= 0);    }


    public static boolean isLinux() {

        String os = System.getProperty("os.name").toLowerCase();        //linux

        return (os.indexOf("linux") >= 0);    }


    public static void addDir(String s) {

        System.out.println("Dir=="+s);        try {

            Field field = ClassLoader.class.getDeclaredField("usr_paths");            field.setAccessible(true);

            String[] paths = (String[]) field.get(null);            for (int i = 0; i < paths.length; i++) {

                if (s.equals(paths[i])) {                    return;

                }            }            String[] tmp = new String[paths.length + 1];

            System.arraycopy(paths, 0, tmp, 0, paths.length);            tmp[paths.length] = s;

            field.set(null, tmp);        } catch (IllegalAccessException e) {

            Logger.getLogger(LibExtPath.class.getName()).log(Level.SEVERE, null,                    "Failed to get permissions to set library path");

        } catch (NoSuchFieldException e) {            Logger.getLogger(LibExtPath.class.getName()).log(Level.SEVERE, null,

                    "Failed to get field handle to set library path");        }

    }}



And this is how I use it:
LibExtPath.configureRXTX();

I think that this could be usefull for other people and maybe could be integrated in the library.



Thanks guys!
And well done.
Ciruman


-----Inline Attachment Follows-----

_______________________________________________
Rxtx mailing list
Rxtx at qbang.org
http://mailman.qbang.org/mailman/listinfo/rxtx



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20101116/2982ad34/attachment-0640.htm>


More information about the Rxtx mailing list