是否可以在执行另一个java应用程序期间创建一个java小程序

发布于 2024-11-02 04:40:23 字数 1938 浏览 2 评论 0原文

我一直在开发一个 Java 应用程序,它执行一系列长查询和计算,并将其结果显示为一系列 HTML 页面。为了可视化图形,我已经使用 JUNG 库一段时间了,看来该库的真正优势在于对用户交互的支持,当图形保存为静态图像(我的 PNG 中)时,这当然是不可用的。案件)。

我想知道是否会:

a)可能

b)可行

c)合理

...在主应用程序执行期间创建一个小程序,然后可以将其插入到HTML报告中,并可以在应用程序完成后以交互方式使用完成执行,用户浏览报告页面。

如果由于技术原因无法做到这一点;对于我如何实现这样的目标,您有任何其他建议/建议吗?

谢谢,

编辑:只是为了澄清这个概念,“主”应用程序是事件链中的一个链接,因此具有如此独立的 GUI。小程序的想法不是模仿或将主应用程序中的所有内容传输到 HTML 页面,而是当用户在执行后查看图形结果时,可以使用 JUNG 库附带的交互式工具主软件已经完成。

如果这个概念仍然相当不清楚,请告诉我,我将再次尝试更详细地解释事情。

更新:按照我得到的建议,感谢@boffinBrain & @AndrewThompson,我编写了我的小程序,并将其与其他可视化相关的类一起放入我的项目中的包中。层次结构如下:

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

现在,HTML 报告是在本地驱动器上的任意位置创建的,这是一项功能,因为用户在运行“主”应用程序之前提供输出文件夹。在我的 ReportGenerator 类(生成这些 HTML 文件)中,我有以下代码:

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 打印输出显示:file:/home/username/workspace/project 这正是我所期望的。在项目文件夹下有 bin/ 和 lib/,在 bin 内部有正确的文件夹层次结构,一直到我的小程序类也存在。

现在我为什么要写下这一切?好吧,因为当我尝试在报告上运行我的小程序时,我得到:

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

我读过类似的问题,例如: 这个这个但问题似乎出在其他地方。我仔细检查了拼写等... 我是否缺少一些简单的东西,或者手头是否有更复杂的问题?

I have been developing a Java application which executes a long series of queries and calculations, and presents its results as a series of HTML pages. For visualizing graphs I have been playing around with JUNG library for a while, and it appears as the real strength of the library is the support for user interaction, which is of course unavailable when the graph is saved as a static image (PNG in my case).

I was wondering if it would be:

a) possible

b) feasible

c) sensible

... to create an applet, during execution of the main application, which then can be insert to the HTML reports and can be used interactively after the application has finished execution and the user goes through the report pages.

If this is not possible due to technical reasons; do you have any alternative recommendations/ suggestions as to how I can achieve something like this?

Thanks,

EDIT: Just to clarify the concept, the "main" application is a link in a chain of events, and thus has so separate GUI. The idea with the applet is NOT to mimic or transport all the stuff from the main app to a HTML page, but to make it possible to use interactive tools that come with JUNG library, when the user is reviewing the graphical results AFTER the execution of the main software has finished.

Let me know if the concept is still rather unclear and I'll give a second attempt to explain things in further detail.

UPDATE: Following the advices I got, thnx to @boffinBrain & @AndrewThompson, I wrote my applet, and placed 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 the HTML reports are created at an arbitrary location on the local drive, this is a feature as the user gives an output folder prior to running 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'd expected. Under the project folder there's bin/ and lib/, and inside bin there is the right hierarchy of folders all the way down to my applet class which also exists.

Now why did I write all this down; well because when I try to run my applet on the reports I get:

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

I have read similar questions like: this or this but it seems like the problem is somewhere else. I double checked the spelling etc...
Is there something simple I am missing, or is there a more complicated problem at hand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2024-11-09 04:40:23

也许这个例子会给你一些可以追求的想法。它创建用作小程序使用的“报告”的数据文件。

因为小程序通过输入文件获取数据,该文件的标题在小程序 param 中指定。数据文件的内容仅受报告要求、您创建报告的技能以及报告的要求的限制。解析它,..以及可用的磁盘空间。 ;)

编译&运行 main(String[]) 以(希望)看到浏览器选项卡中打开的 2 个网页。

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;
    }
}

关于进一步的问题:

..给定的代码是否假定 html 报告和小程序位于同一文件夹中?

未必。 input 参数可能会为 other 目录指定 ../other/data3.txt,该目录与 ../other/data3.txt 目录位于同一级别。 >HTML 或 /reports/data3.txt 用于站点根目录的 reports 目录。

..正如您还指出的,在现实生活中的示例中,小程序的代码很可能位于其自己的类中,这是否会导致如何将其合并到 html 文件中(这些文件是在一个名为 ReportGenerator 的单独类中生成)?

只需稍作更改即可指向 applet.jar,而不是 application.jar。使用codebaseHTMLapplet.jar的目录分开(尽管也可以通过相对或绝对URL访问档案)。

Maybe this example will give you some ideas to pursue. It creates data files used as 'reports' for consumption by the applet(s).

Because the applet gains the data via an input file whose title is specified in an applet param. The content of the data file is only limited by the requirements of the report, your skill to create it & parse it, ..and available disk space. ;)

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

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;
    }
}

In relation to further questions:

..does the given code assume that the html reports and the applet are located in the same folder?

Not necessarily. The input parameter might specify ../other/data3.txt for the other directory at the same level as the one contained by the HTML, or /reports/data3.txt for a reports directory at the root of the site.

..As you have also noted, in a real-life example the code for the applet would most likely be in its own class, would that pose any complications as to how it would be incorporated into the html files (which are generated in a separate class, named ReportGenerator)?

It would require only slight changes to point to the applet.jar as opposed to the application.jar. Use a codebase to separate the HTML from the directory of the applet.jar (though archives can also be accessed via relative or absolute URLs).

儭儭莪哋寶赑 2024-11-09 04:40:23

创建一个小程序来显示数据绝对是可行的,但您不想每次都动态生成一个新的。您想要创建一个单独的、独立的小程序,它可以根据一组文本格式的输入数据生成图形/报告,然后在创建 HTML 页面时,使用小程序参数(使用 PARAM 标记)提供报告数据)。

It's definitely feasible to create an applet to display the data, but you don't want to dynamically generate a new one each time. You want to create a separate, stand-alone applet which can generate your graphs/reports from a set of input data in text format, and then when you create the HTML page, supply the report data using an applet parameter (using the PARAM tag).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文