Is it possible to create a java applet at runtime of another java application

I am developing a Java application that performs a long series of queries and calculations and presents its results as a series of HTML pages. For visualization of graphs, I played with the JUNG library for some time, and it seems that the real strength of the library is support for user interaction, which, of course, is not available when the graph is saved as a static image (PNG in my case).

I was wondering if this would be:

a) possibly

b) acceptable

c) reasonable

... to create an applet at runtime of the main application, which can then be inserted into HTML reports and can be used interactively after the application completes, and the user views the report pages.

If this is not possible for technical reasons; Do you have any alternative recommendations / suggestions on how I can achieve something like this?

Thanks,

EDIT:. To clarify the concept, the β€œmain” application is a link in the chain of events and therefore has such a separate graphical interface. The idea with the applet is NOT to imitate or transfer all materials from the main application to the HTML page, but to use the interactive tools that come with the JUNG library when the user views the graphic results AFTER the main software is over.

Let me know if the concept is still unclear, and I will give a second attempt to explain things in more detail.

UPDATE: Following the tips I got, thnx to @boffinBrain and @AndrewThompson, I wrote my applet and put it in a package in my project along with other visualization-related classes. The hierarchy is as follows:

my.domain.project my.domain.project.tests my.domain.project.visualization 

Now HTML reports are created in any place on the local disk, this is a function, since the user gives an output folder before starting the "main" application. In my ReportGenerator class (which generates these HTML files) I have the following bit of code:

 File bin = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toString()); String codebase = bin.getParent(); System.out.println(codebase); String archive = "lib/collections-generic-4.01/collections-generic-4.01.jar"; String applet_name = "bin/my.domain.project.visualization.HierarchyGraphApplet.class"; 

codebase printout shows: file:/home/username/workspace/project , which is correct, what I expected. There is bin / and lib / in the project folder, and inside bin there is the correct folder hierarchy right up to my applet class, which also exists.

Now why did I write all this? because when I try to run my applet in reports, I get:

 java.lang.NoClassDefFoundError: bin/my/domain/project/visualization/HierarchyGraphApplet (wrong name: my/domain/project/visualization/HierarchyGraphApplet) 

I read similar questions like this or, but it looks like the problem is somewhere else, I double-checked spelling, etc ... Is there something simple that I am missing, or is there more a difficult problem?

+4
source share
2 answers

Perhaps this example will give you some ideas to continue. It creates data files used as β€œreports” for consumption by applets.

Since the applet receives data through the input file, the name of which is indicated in the param applet. The content of the data file is limited only by the requirements of the report, your ability to create and analyze it, and the available disk space .;)

Compile and run main(String[]) to (hopefully) see 2 web pages open in your browser tabs.

 import java.awt.Desktop; import javax.swing.*; import java.net.*; import java.io.*; /** Simplistic example, not intended to show good I/O practices or Exception handling for the sake of brevity. */ public class Reporter extends JApplet { public void init() { String input = getParameter("input"); JEditorPane report = new JEditorPane(); report.setText("Problem loading input file"); add(report); URL url; try { url = new URL(getDocumentBase(), input); report.setPage(url); } catch(Exception e) { e.printStackTrace(); } } /** The main represents our report generator. It is part of the applet class only in order to create an SSCCE. Good design would imply that it might be in a class ReportGenerator, while the applet is in class ReportViewer. */ public static void main(String[] args) throws Exception { File f; String title = "1"; String data = "apples"; createInput(title, data); f = createHTML(title); Desktop.getDesktop().browse(f.toURI()); title = "2"; data = "oranges"; createInput(title, data); f = createHTML(title); Desktop.getDesktop().browse(f.toURI()); System.out.println( "End of report generation.." ); } public static void createInput(String title, String data) throws Exception { File f = new File("data" + title + ".txt"); PrintWriter pw = new PrintWriter(f); pw.println(data); pw.flush(); pw.close(); } public static File createHTML(String title) throws Exception { File f = new File("Data" + title + ".html"); PrintWriter pw = new PrintWriter(f); pw.println("<html>"); pw.println("<title>"); pw.println("Data " + title); pw.println("<title>"); pw.println("<body>"); pw.println("<h1>"); pw.println("Data " + title); pw.println("</h1>"); pw.println("<applet "); pw.println("code='Reporter'"); pw.println("width='400'"); pw.println("height='400'"); pw.println(">"); pw.println("<param name='input' value='data" + title + ".txt'>"); pw.println("</applet>"); pw.println("</body>"); pw.println("</html>"); pw.flush(); pw.close(); return f; } } 

For further questions:

.. does this code contain that the html reports and the applet are in the same folder?

Not necessary. The input parameter can specify ../other/data3.txt for the other directory at the same level as the one in the HTML directory or /reports/data3.txt for the reports directory in the root of the site.

. As you already noted, in a real example, the code for the applet will most likely be in its own class, will this create any complications as to how it will be included in html files (which are generated in a separate class called ReportGenerator)?

This will require only minor changes pointing to applet.jar , not application.jar . Use codebase to separate HTML from the applet.jar directory (although archives are also available through relative or absolute URLs).

+3
source

It is definitely possible to create an applet to display data, but you do not want to dynamically generate a new one each time. You want to create a separate standalone applet that can generate your graphs / reports from a set of input data in text format, and then when you create an HTML page, send report data using the applet parameter (using the PARAM tag).

+2
source

All Articles