1

I have two applications: (1) a JavaFX application A and (2) a purely JavaScript web application B (no backend) accessed either as local files (for developers) or at a certain URL (for the users).

In A the user can save a file and then open B in the user's browser via Desktop.getDesktop().browse(). Then the user can load that file in B by clicking on an input element of type file, which allows the usage of FileReader.

That process is too cumbersome for the users, so I want to remove that requirement of the user to save and load a file.

My problem is: How can I transfer this data from A to B now?

  • GET parameters only allow a few thousand characters, which is not enough for us
  • I see no way to pass POST parameters from Java to the browser and also to access those parameters from within JavaScript
  • FileReader cannot read files using path names because of security
  • we can't use platform or browser dependent native code solutions because the users use different OSes and browsers
  • the web application uses many libraries and advanced JavaScript features, which I don't expect a JavaFX HTML window to support

Is there any way to get one of the above methods to work or is there any alternative to pass data between Java and a purely client-based JavaScript application in a browser?

Update

As suggested in the accepted answer, I serve the data as using Jetty.

1. Java: Define JSON Server

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.json.JSONArray;

public class JsonServer extends Server
{
    static final int PORT = 1234;  

    public JsonServer(JSONArray json)
    {
        super(PORT);
        setHandler(new JsonHandler(json));
    }

    public static class JsonHandler extends AbstractHandler
    {
        final JSONArray json;

        public JsonHandler(JSONArray json)
        {
            this.json = json;
        }

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            response.setContentType("application/json; charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);

            try(PrintWriter out = response.getWriter();)
            {
                out.println(json);
            }
            baseRequest.setHandled(true);
        }

    }
}

2. Java: Start Server

            Server server = new JsonServer(json);
            server.start();
            browse("https://myapp.com?load=http://localhost:1234");

3. JavaScript: Fetch

    const load = url.searchParams.get("load");
    const json = await (await fetch(load)).json();
10
  • 1
    Without a backend to application B, you have no other options as far as I can see. Have you actually tried the JavaFX HTML window though? If it relies on a standard browser rendering engine behind it then you might find it's ok Commented Sep 3, 2019 at 14:25
  • Another thought: What does application B actually do with the files the user loads into it? Could you not just implement the same feature in the Java application? I'm not seeing what the advantage of a separate web application is if it doesn't have a server backend of any kind Commented Sep 3, 2019 at 14:29
  • @ADyson: Application B was developed over many months, it would be too much effort to re-implement it in Java just for A, which is a much smaller tool. Commented Sep 3, 2019 at 14:30
  • You could dynamically generate an HTML file in your Java app that would contain the Web app (javascript) and the data it needs, and invoke that file from the Java app. Commented Sep 3, 2019 at 14:34
  • @georg: That would be useful if A was big and B very small but it is the other way around. B is updated often and uses npm, babel and so on. It would be too much hassle to include it on the users PC and update and build it. Commented Sep 3, 2019 at 14:37

1 Answer 1

1

If you assume that the web page is always going to be viewed on the same machine that is running the JavaFX application you could always have it export the file via HTTP (using Jetty or similar), then have the web page load the data using a URL such as "http://localhost:port/fileContainingData".

Sign up to request clarification or add additional context in comments.

1 Comment

The webpage will indeed be viewed on the same machine, this is a great suggestion, I will try it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.