The Artima Developer Community
Sponsored Link

Java Buzz Forum
POSIX from Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
POSIX from Java Posted: Jan 10, 2012 10:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: POSIX from Java
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

I have been doing more traditionally unix-y stuff from Java lately, and one of the things I have needed is proper access to POSIX and libc system calls. Luckily, there are now a couple fabulous libraries to make this easy – no more need to do your own JNI muckery.

I’ve been using jnr-posix with great success. Using it for something like execv(3) looks like:

POSIX posix = POSIXFactory.getPOSIX(new POSIXHandler()
{
    @Override
    public void error(Errno errno, String s)
    {
    }

    @Override
    public void unimplementedError(String s)
    {
    }

    @Override
    public void warn(WARNING_ID warning_id, String s, Object... objects)
    {
    }

    @Override
    public boolean isVerbose()
    {
        return false;
    }

    @Override
    public File getCurrentWorkingDirectory()
    {
    return new File(".");
    }

    @Override
    public String[] getEnv()
    {
        return new String[0];
    }

    @Override
    public InputStream getInputStream()
    {
        return System.in;
    }

    @Override
    public PrintStream getOutputStream()
    {
        return System.out;
    }

    @Override
    public int getPID()
    {
        return 0;
    }

    @Override
    public PrintStream getErrorStream()
    {
        return System.err;
    }

}, true);
    
String[] args = new String[] {
  "/usr/bin/ssh", "lasker.skife.org"
};
posix.execv("/usr/bin/ssh", args);

The bulk of that snippet is setting up the POSIXHandler which provides nice callbacks for the things that are not obvious how to handle, or might want to be overidden in a specific environment. The boolean flag at the end says to use the native POSIX implementation rather than emulating it in Java. The library will sniff your system and dynamically link the right things – is very nice.

The library doesn’t properly declare its dependencies in its pom, so if you want to use it you need to depend on each of:

<dependency>
    <groupId>com.github.jnr</groupId>
    <artifactId>jnr-posix</artifactId>
    <version>2.0</version>
</dependency>

<dependency>
    <groupId>com.github.jnr</groupId>
    <artifactId>jnr-ffi</artifactId>
    <version>0.6.0</version>
</dependency>

<dependency>
    <groupId>com.github.jnr</groupId>
    <artifactId>jnr-constants</artifactId>
    <version>0.8.2</version>
</dependency>

The jnr-posix pom lists jnr-constants and jnr-ffi as provided for some reason. Hopefully that will be remedied in $version.next().

Read: POSIX from Java

Topic: #800! and #801! Post CBC Previous Topic   Next Topic Topic: What I Learnt about JavaFX Today

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use