Skip to content

Conversation

@rijulshrestha
Copy link
Contributor

I've updated the structure, a bit of text and tried to make the code examples simpler and easy to follow.

@rijulshrestha rijulshrestha self-assigned this Nov 12, 2025
@rijulshrestha rijulshrestha added the documentation Improvements or additions to documentation label Nov 12, 2025
@rijulshrestha rijulshrestha marked this pull request as ready for review November 12, 2025 01:13
@rijulshrestha
Copy link
Contributor Author

Considering to change the "Mounting points overview" callout to normal bullet points.

Copy link
Member

@epanholz epanholz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments. Overall it already looks way better but some parts can still be improved,

Comment on lines 2 to 7
- sites/*
- packages/*

ignoredBuiltDependencies:
- esbuild
- sharp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why these changes are necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've forced build everything on my machine and it got committed on accident. I've reverted the changes.

## Getting a file from the Virtual File System to JavaScript (i.e local file system)

## 2\. Getting Files from the Virtual Filesystem to the Local Filesystem (Java to JavaScript)
When Java runs under CheerpJ, files are saved in the virtual filesystem (typically under **`/files/`**), not directly to the user’s disk. To make a file available to the page (for download, upload, or preview), expose it via a **Java native method** implemented in JavaScript. The JavaScript side reads the bytes from the VFS and then uses standard browser APIs (e.g., create a `Blob`, trigger a download, or upload it).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(typically under /files/) -> (defaults to /files/)

I would also rewrite this slightly:
To make a file available to the page (for download, upload, or preview), expose it via a Java native method implemented in JavaScript. The JavaScript side reads the bytes from the VFS and then uses standard browser APIs (e.g., create a Blob, trigger a download, or upload it).

Something like this:
CheerpJ provides APIs that are available through JavaScript to extract files from the virtual file system (link to blob api). To implement a more seamless file interaction you can utilize JavaScript native methods (link to docs) to handle the file after a filer interaction. This can look something like this: (Example flow you already have)

We should also add the cjFileBlob() api here as method 1, and then method 2 is with javascript natives.

@rijulshrestha rijulshrestha force-pushed the filesystem-guide-rework branch from 2859d5e to 24a37ef Compare November 13, 2025 15:59
@cloudflare-workers-and-pages
Copy link

Deploying labs-browserpod-previews with  Cloudflare Pages  Cloudflare Pages

Latest commit: 24a37ef
Status:🚫  Build failed.

View logs

@rijulshrestha rijulshrestha force-pushed the filesystem-guide-rework branch from 24a37ef to 7f05140 Compare November 13, 2025 16:21
Comment on lines +45 to +55
Entries you create under **`/str/`** (from JavaScript) are readable from Java as regular files. Remember: `/str/` is **not persisted** and is **read-only** from Java.

```java title="App.java"
import java.nio.file.Files;
import java.nio.file.Path;
/*
Rest of your Java code...
*/
String text = Files.readString(Path.of("/str/fileName.txt"));
System.out.println(text);
```
Copy link
Contributor Author

@rijulshrestha rijulshrestha Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this code look? Do I need to add more structure and details like below;

import java.io.File;                  // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner;             // Import the Scanner class to read text files

public class ReadFile {
  public static void main(String[] args) {
    File myObj = new File("filename.txt");

    // try-with-resources: Scanner will be closed automatically
    try (Scanner myReader = new Scanner(myObj)) {
      while (myReader.hasNextLine()) {
        String data = myReader.nextLine();
        System.out.println(data);
      }
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part looks good now.

- **`cheerpjRunLibrary("")`**: This creates a library mode object that allows you to use Java's `nio.file` APIs in JavaScript. You can read more about library mode in the [CheerpJ documentation](/docs/reference/cheerpjRunLibrary).
- **`Files.copy(source, target, [StandardCopyOption.REPLACE_EXISTING])`**: This copies the file from the `/app/` mount point to the `/files/` mount point. The `REPLACE_EXISTING` option ensures that if the target file already exists, it will be overwritten.
- **Accessing the File in Java**: After copying, your Java application can access the file simply by using `new File("notes_tmp.txt")`, without needing to specify the `/app/` prefix.
When Java runs under CheerpJ, files are saved in the virtual filesystem (defaults under **`/files/`**), not directly to the user’s disk. Use the JavaScript-accessible [cjFileBlob API](/docs/reference/cjFileBlob) to extract files from the virtual file system. For a more seamless flow, define a [Native Methods](/docs/guides/implementing-native-methods) implemented in JavaScript to process the file. This can look like:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've linked to the docs here and have removed the 'Notes' part in the end where the docs were initially linked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is okay. I originally suggested to add a Method 1 - Using cjFileBlob and a Method 2 - Using native methods to make it more align with the previous section and also a bit easier to read. cjFileBlob can be used to extract files from the virtual file system and having a very short stand alone example here would be nice.

Copy link
Member

@epanholz epanholz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested a few more small changes, after that it should be ready for merging.

**Mounting points overview**

CheerpJ's virtual filesystem has different **mounting points**, each with specific purposes:
- **`/app/`** — Read-only mount mapping to your web server’s root (HTTP assets). Java can read the files using the '/app/' prefix.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read-only mount mapping to your web server’s root (HTTP assets). Java can read the files using the '/app/' prefix. -> Read-only mount that points to the root of your web server. Java can read the files using the '/app/' prefix.

I think saying HTTP assets might confuse users, since they can pass any files they want to the application like this, like config files or similar.

To access local files, first import them into the virtual file system (see previous section). To save or download files to the user’s machine from Java, you can utilize a JavaScript native method again.

// (Retain the native method declaration from Section 2)
**Java Source Code: Essential `JFileChooser` and Download Logic**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java Source Code: Essential JFileChooser and Download Logic -> Java Source Code: JFileChooser and download logic

System.out.println("Triggered direct URL download: " + fileUrl.toString());
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error downloading from URL: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this line, not need to add the reference to JOptionPane if it does not show in the code example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants