SoFunction
Updated on 2025-04-07

Detailed explanation of the usage of JCIFS library used in SMB/CIFS network in Java

1. Introduction

In network communication, the Server Message Block (SMB) protocol plays an important role in realizing file sharing and communication between computers. For Java developers, using SMB can be challenging due to the lack of built-in support for this protocol. At this time, JCIFS (Java CIFS Client Library) comes in handy. JCIFS is a powerful library that allows Java applications to seamlessly interact with SMB/CIFS resources. This article will explore the concept of JCIFS, how it works, and how to use it effectively in Java applications.

2. What is JCIFS?

JCIFS is an open source Java library designed to implement the SMB/CIFS network protocol. It allows Java applications to access shared files and printers on the network, making it easier to integrate with Windows-based systems. The library supports a variety of SMB features, including authentication, file operations, and directory browsing.

Key Features

  • SMB/CIFS protocol support: JCIFS supports SMB1 and SMB2 protocols, enabling it to communicate with a variety of SMB servers, including Windows and Samba.
  • Authentication: This library provides support for NTLM (NT LAN Manager) authentication, allowing secure access to shared resources.
  • File Operation: JCIFS allows developers to perform various file operations, such as reading, writing, deleting, and listing files in shared directories.
  • Catalog browsing: This library enables users to browse shared directories and retrieve information about files and subdirectories.
  • Cross-platform compatibility: As a Java library, JCIFS is platform-independent and suitable for use in multiple environments.

4. Set up SMB Sharing on Windows

Open Control Panel

Enter Control Panel in the Windows Search box and open it.

Enter "Program"

Click Programs and select Enable or turn off Windows features.

Enable SMB 1.0/CIFS file sharing support(If needed):

In the Windows Features window, find "SMB 1.0/CIFS File Sharing Support" to make sure it is checked. If you need to use a newer SMB version (such as SMB 2.0 or SMB 3.0), you do not need to enable this option.

Create a shared folder

  • In File Explorer, find the folder you want to share, right-click it, and select "Properties".
  • Go to the Share tab and click Advanced Sharing.
  • Check "Share this folder" and set the share name.
  • Click "Permissions" to set user permissions, add the required users and set the corresponding permissions (read, write, etc.).

Set up network discovery and file sharing

  • In the Control Panel, go to Network and Sharing Center.
  • Click "Change Advanced Sharing Settings".
  • Make sure the "Open Network Discovery" and "Open File and Printer Sharing" options are selected.

Accessing shared folders

Other computers can enter\\<windows-ip>\<shared-folder>to access the shared folder.

5. How to get started with JCIFS

To use JCIFS in a Java project, follow these steps:

Add JCIFS dependencies: If you use Maven, you canAdd the following dependencies to:

<dependency>
    <groupId>jcifs</groupId>
    <artifactId>jcifs</artifactId>
    <version>1.3.17</version>
</dependency>

Basic examples: Here is a simple example of how to list files in shared folders using JCIFS:

package ;

import ;
import ;
import ;

import ;
import ;

public class SmbFileReader {
    public static void main(String[] args) {
        String user = "dell"; // Username for the shared folder
        String password = "abc000000"; // Password for the shared folder
        String sharedFolderUrl = "smb://BJDPLHHUAPC/test/"; // URL of the shared folder

        // Create an authentication object
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user + ":" + password);

        try {
            // Create an SmbFile object for the shared folder
            SmbFile sharedFolder = new SmbFile(sharedFolderUrl, auth);

            // List the files in the shared folder
            SmbFile[] files = ();
            for (SmbFile file : files) {
                ("File: " + ());

                // If it is a file, read its content
                if (!()) {
                    try (SmbFileInputStream fis = new SmbFileInputStream(file);
                         BufferedReader reader = new BufferedReader(new InputStreamReader(fis))) {
                        String line;
                        // Read the file line by line
                        while ((line = ()) != null) {
                            (line); // Print each line
                        }
                    }
                }
            }
        } catch (Exception e) {
            (); // Print the stack trace in case of an exception
        }
    }
}

Running example: Make sure you have the correct credentials and shared folder URLs. Compile and run the Java program to view the list of files in the specified shared folder.

File: file_utils/
File: file_utils1/
File: 
this is a test

The above are just some key codes. Please refer to the code repository below.

Code Repository

/Harries/Java-demo(JCIFS)

Use scenarios

  • File Management Application: JCIFS can be used to create applications that manage files on remote SMB servers, allowing users to upload, download, and organize files.
  • Backup Solution: Developers can use JCIFS to create backup solutions that store data on network SMB shares.
  • Integrate with legacy systems: JCIFS can help integrate Java applications with legacy systems that rely on SMB for file sharing.

7. Conclusion

JCIFS is a very valuable tool for Java developers and is able to interact with SMB/CIFS resources. Its powerful features and ease of use make it an excellent choice for applications that require file sharing and network communication. By understanding how to effectively leverage JCIFS, developers can create powerful applications that can integrate seamlessly with Windows-based environments. Whether you are building a file management system, backup solutions, or simply need access to shared resources, JCIFS provides the features you need to succeed.

This is the end of this article about the usage of the JCIFS library used in Java for SMB/CIFS network. For more related Java JCIFS content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!