简体   繁体   中英

Best way to accees NAS in java: Mount using external Linux command “mount”

I need to store files on a network drive (MyBook Live). At the moment i'm doing this:

String cmd = "mount //192.167.0.3/Public/Folder /media/Folder -o user=test,password=test"; 
Runtime run = Runtime.getRuntime() ; 
Process pr = run.exec(cmd); 
pr.waitFor();

Is there a better way to do it?

Did the same thing - executing a command to do the mount - for a project I was working on last fall. There are no Java API to do this directly (or any better way as far as I know). It worked properly.

I agree with Bailey S recommendation of an fstab entry - did the same thing.

You might want to read on the pitfalls of using Runtime.exec() and Process.waitFor() ( http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=2 ). I prefer using Apache Commons Exec ( http://commons.apache.org/proper/commons-exec/ ) for executing OS commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM