To get SMBJ, you have two options:
-
Add a dependency to SMBJ to your project.
-
Build SMBJ yourself.
And, if you want, you can also run the SMBJ examples.
Binary releases of SMBJ are not provided here, but you can download it straight from the Maven Central repository if you want to.
A - Listing Files on a Share/Folder
SMBClient client = new SMBClient();
try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.TXT")) {
System.out.println("File : " + f.getFileName());
}
}
}
B - Deleting a file
SMBClient client = new SMBClient(config);
try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
share.rm("FILE");
}
}
C - Adjusting Timeout and Socket Timeout
SmbConfig config = SmbConfig.builder()
.withTimeout(120, TimeUnit.SECONDS) // Timeout sets Read, Write, and Transact timeouts (default is 60 seconds)
.withSoTimeout(180, TimeUnit.SECONDS) // Socket Timeout (default is 0 seconds, blocks forever)
.build();
SMBClient client = new SMBClient(config);
try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
...
}
}
If you’re building your project using Maven, you can add the following dependency to the pom.xml
:
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId>
<version>0.4.0</version>
</dependency>
If your project is built using another build tool that uses the Maven Central repository, translate this dependency into the format used by your build tool.
-
Clone the SMBJ repository.
-
Ensure you have Java7 installed with the Unlimited strength Java Cryptography Extensions (JCE).
-
Run the command
./gradlew clean build
.
The implementation is based on the following specifications: