java web start部署属性文件读取

标签 java file properties java-web-start

我正在尝试在 Java Web Start 中部署一个非常简单的应用程序。我对此完全陌生。

我的应用程序包含一个 Java 文件。通过 java (java CustomDemo) 运行应用程序时,它会显示一个包含按钮的对话框。当用户单击该按钮时,将读取属性文件,并且 Hello World 将作为标签显示在对话框上。

  • N.B:在一个文件夹中我有 java 类以及 .properties 文件。

我想在 Web Start 中部署该应用程序。

我遵循的步骤。

  1. 我已经制作了我的应用程序的 jar (jar -cvf SampleDemo.jar CustomDialog.class)。
  2. 我已经编写了 jnlp 文件。
  3. 我创建了一个index.html页面
  4. 将整个内容保存在 tomcat/webapps 中并在 tomcat 中部署。

现在的问题是,如果我将标签显示为任何硬编码字符串,那么应用程序就会像魅力一样工作。但是,当我读取属性文件时,我在 Java Web Start 中运行时遇到异常“File Not Found Eception”

我的示例代码如下

自定义对话框.java

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTable;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class CustomDialog extends JDialog implements ActionListener
{
    protected boolean i_boolButtonClicked = false;
    protected String LABEL                = "";

    public CustomDialog()
    {
        this.setSize(500, 300);
        JButton but = new JButton("Hello");


        //Start Anjan to read data/text from .properties file..
        Properties  i_propConfig        = new Properties();
        try
        {
            FileInputStream inStream = new FileInputStream("./Test.properties");
            i_propConfig.load( inStream  );
            inStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        String l_strKey = "";
        String l_strVal = "";

        Enumeration l_enum = i_propConfig.keys();
        while(l_enum.hasMoreElements())
        {
            l_strKey = (String)l_enum.nextElement();

            if(l_strKey == null || l_strKey.equals( "" ))
                continue;

            l_strVal = i_propConfig.getProperty( l_strKey );

            if(l_strVal == null || l_strVal.equals( "" ))
                continue;
        }

        System.out.println("Properties read from file--> Key: "+l_strKey +" Value: " +l_strVal);
        LABEL = l_strVal;
        //End Anjan to read data/text from .properties file..

//      but.addActionListener(new ActionListener() 
//      {
//          public void actionPerformed(ActionEvent e) 
//          {
//              //getContentPane().add(new JLabel("Hello World"));
//              getContentPane().add(new JLabel(LABEL));
//              getContentPane().validate();
//          }
//      });

        but.addActionListener(this);

        Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();          
        this.setLocation((int)(dim.width- getWidth ())/2,(int)(dim.height-this.getHeight ())/2);

        but.setSize(600, 5);
        this.add(but);
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setVisible(true);
    }

    public static void main(String[] args) 
    {
        CustomDialog l_objCustomDialog = new CustomDialog();
    }

    protected void processWindowEvent(WindowEvent e) 
    {
        super.processWindowEvent(e);

        if (e.getID() == WindowEvent.WINDOW_CLOSING) 
        {
            setVisible(false);
            System.exit(0);
        }
    }

    public void actionPerformed(ActionEvent e) 
    {
        System.out.println("Hello button clicked......");
        getContentPane().add(new JLabel(LABEL));
        getContentPane().validate();
    }
}

SwingDemo.jnlp

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="http://172.28.1.139:8400/SwingDemo" href="SwingDemo.jnlp">
    <information>
        <title>Swing Demo</title>
        <vendor>Swing</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
        <jar href="SwingDemo.jar" main="true" download="eager" />
    </resources>

    <application-desc
         name="SwingDemo Demo Application"
         main-class="SwingDemo.CustomDialog"
         width="300"
         height="300">
     </application-desc>
     <update check="background"/>

<security>
    <all-permissions/>
</security>
</jnlp>

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
    <script src="http://www.java.com/js/deployJava.js"></script>
    <script>
        // using JavaScript to get location of JNLP file relative to HTML page
        var dir = location.href.substring(0,location.href.lastIndexOf('/')+1);
        var url = dir + "SwingDemo.jnlp";
        deployJava.createWebStartLaunchButton(url, '1.6.0');
    </script>
 </BODY>
</HTML>

几天来我只是在挖掘,但找不到任何解决方案。我认为必须有其他方式来读取 web start 中的 .properties 文件。任何人都可以提出任何清晰而明智的方法来解决问题。

还有一件事我不想将属性文件固定在我的 jar 中。即使我也尝试过这种方式。

最佳答案

修复 JWS 应用程序中的 FileNotFoundException 的方法。就是通过URL访问资源。

如果属性文件位于 JWS 应用程序的运行时类路径上,则可以使用 getClass.getResource(String) 形成该 URL。 (在 JNLP 中 jar 元素引用的 Jar 中)。

如果服务器上的资源是松散的,则可以相对于代码库或文档库(如果是小程序)形成 URL。

请注意,URL 实际上意味着“只读”,而不是“读/写”。在属性发生变化的情况下,我们需要采取更复杂的策略来在本地序列化它们。

关于java web start部署属性文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14058640/

相关文章:

java - 内存不足错误 : Java Heap space

c++ - C++中STL::map数据的随机文件访问

java - 使用 FileOutputStream 时的双文件名

Java 属性注释标准/库

javascript - 以声明方式分配 javascript 数组属性

java - 为什么我必须覆盖 toString 方法而不是仅仅创建另一个方法?

java - 如何在不移动放大镜的情况下仅对齐 SearchView 的提示文本?

JavaScript:如何为函数设置属性?

java - 如何在 LibGDX 中获取模型实例的 3d 坐标?

java - 如何使用Java在线下载mp3文件?