相关文章推荐
追风的炒粉  ·  portError: ...·  2 周前    · 
阳刚的蚂蚁  ·  Linux ...·  昨天    · 
知识渊博的斑马  ·  playframework - Scala ...·  1 年前    · 
俊秀的葫芦  ·  sqlserver中sp_executesq ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to access a NFS share on a CentOS 6.3 System from within a Java application. I've tried the following libraries but can't get either to work:

YaNFS

Trying to access the NFS Share with YaNFS I run into an NfsException with ErrorCode 10001 (NFSERR_BADHANDLE). Sometimes the text of the Exception says "Stale NFS file handle". My code for YaNFS is:

    public static void main(String[] args) {
    XFile xf = new XFile("nfs://192.168.1.10/nfs-share");
    nfsXFileExtensionAccessor nfsx =
        (nfsXFileExtensionAccessor)xf.getExtensionAccessor();
        if (! nfsx.loginPCNFSD("192.168.1.10", "rpx-nfs-user", "Test123!")) {
             System.out.println("login failed");
             return;
        if (xf.canRead())
             System.out.println("Read permission OK");
             System.out.println("No Read permission");

nfs-client-java

Trying to initialize Nfs3 object with "nfs-client-java" I get a MountException that looks like:

com.emc.ecs.nfsclient.mount.MountException: mount failure, 
    server: 192.168.1.205, 
    export: /home/share, 
    nfs version: 3, 
    returned state: 13
at com.emc.ecs.nfsclient.nfs.nfs3.Nfs3.lookupRootHandle(Nfs3.java:359)

State 13 on this point says Permission denied.

I can access this share from another CentOS-System (with authorized to access to this folder uid and gid) by mounting this share and from Windows-System(with authorized to access to this folder login and password) as well.

Is there anyone, who has already solved this problem? Or maybe someone can help me get further?

So to make clear, you can mount this nfs share from command line? If so, why not just mount it and then read things from the mount point as if they were local files? – ashbygeek Jan 19, 2017 at 12:08 Unfortunately, it is not possible because I am not the operator of the system, where my application is running. I suppose the operator cannot do it because of cluster-system where application is running. On my question to mount it I get rejection, so I must do it programmatically. – AndySpu Jan 19, 2017 at 12:15 Can you post the YaNFS code you wrote? Having the exception is good, but its only half the battle. – ashbygeek Jan 19, 2017 at 12:28 also, since YaNFS is apparently an open source continuation of WebNFS, the webnfs documentation should be helpful: docs.oracle.com/cd/E19455-01/806-1067/6jacl3e6p/index.html – ashbygeek Jan 19, 2017 at 12:30 my YaNFS code looks like this: XFile xf = new XFile("nfs://192.168.1.10/nfs-share/test_file.txt"); System.out.println(xf.canRead()); here I get false on calling canRead(). But I have debuged the scope and come to lookup method in Nfs3 Class where i get the Exception mentioned above. – AndySpu Jan 19, 2017 at 13:09

So, you indicate in a comment that you need a specific user's priveledges to be able to access the files, hence the 'Permission Denied' error. In YaNFS you need to look at the nfsXFileExtensionAccessor to be able to send the username and password so you can gain permission. Here's an example I pulled from this page: https://docs.oracle.com/cd/E19455-01/806-1067/6jacl3e6g/index.html

import java.io.*;
import com.sun.xfile.*;
import com.sun.nfs.*;
public class nfslogin {
     public static void main(String av[])
          try {
               XFile xf = new XFile(av[0]);
               com.sun.nfsXFileExtensionAccessor nfsx =
               (com.sun.nfsXFileExtensionAccessor)xf.getExtensionAccessor();
               if (! nfsx.loginPCNFSD("pcnfsdsrv", "bob", "-passwd-")) {
                    System.out.println("login failed");
                    return;
               if (xf.canRead())
                    System.out.println("Read permission OK");
                    System.out.println("No Read permission");
          } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace(System.out);

No, I don't think you're supposed to call bind() before the login operation. From the XFile sourcecode on github:

* Check that the file is open. * The open() method must be called before * any other methods in the Accessor. * This makes it easier for Accessors to * centralize initialization code in one place. private boolean bind() { if (bound) return true; bound = xfa.open(this, false, false); return bound;

Since the bind() function is trying to open the file, it will always fail until you authenticate. Then consider this code from the XFileExtensionAccessor code on github:

* Sets the user's RPC credential from Login name and password. * Every NFS request includes a "credential" that identifies the user. * An AUTH_SYS credential includes the user's UID and GID values. * These are determined from the user's login name (and password) * by the PCNFSD service that must be available on a local server. * Once the credential is set, it is assigned globally to all * future NFS XFile objects. * If this method is not called, a default credential is assigned * with a UID and GID of "nobody". * @param <code>host</code> The name of the host that runs the PCNFSD service. * This does not have to be an NFS server. * @param <code>username</code> The user's login name. * @param <code>password</code> The user's password. * This is obscured before transmission to the PCNFSD server. * @return true if the login succeeded, false otherwise. public boolean loginPCNFSD(String host, String username, String password) { return NfsConnect.getCred().fetchCred(host, username, password);

So, the loginPCNFSD() function sets credentials for the XFile system globally until you logout or use a new login. Definitely call it before calling XFile.Bind();

Thank you for this information. Have you tried this source code? I've tried but I can not bind the file. There is bind()-Method in the class XFile. It will be always called before any operation will be execute (e.g. loginPCNFSD()). Thid method returning always false by me. – AndySpu Jan 23, 2017 at 7:29 Well, the updated looks really ugly here since I'm showing some code and stuff, so I'll just update the answer. – ashbygeek Jan 23, 2017 at 11:49 it is not a problem for me to use uid and gid. I know this data. "fetchCred" try to get this using some Unix-Service(btw. if it is not running, logil will get root id's to try). I have updated my source code to YaNFS part. In it I allways run into System.out.println("login failed") message – AndySpu Jan 26, 2017 at 13:47 So does that mean we can access a NFS share using CIFS/SMB protocol i.e using a library like jcifs.org, jcifs-codelibs? – Chidambaram Palaniappan Jan 12, 2021 at 1:56

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.