Java web启动部署属性文件读取



我正在尝试在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文件。我创建了一个index.html页面
  3. 把所有的东西都放在tomcat/webapps中,并部署在tomcat中。

现在的问题是,如果我将标签显示为任何硬编码字符串,那么应用程序就会像魅力一样工作。但只要我阅读属性文件,我得到异常,而在Java Web启动运行的"文件未发现异常"

我的示例代码如下

CustomDialog.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>

几天来我一直在挖,找不到任何解决办法。我认为一定有一些其他的方式来读取。properties文件在web启动。谁能提出一个明确而聪明的方法来解决这个问题?

还有一件事,我不想把属性文件固定在我的jar中。我也试过了

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

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

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

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

@Andrew Thompson我已经按照你提到的方式加载了资源。代码片段如下:

String url = "Test.properties";
System.out.println("Before printing paths..");
System.out.println("Path2: "+ getClass().getResource(url).getPath());
FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
i_propConfig.load(inputStream);
inputStream.close();

我已经在eclipse中配置了层次结构(在source下有一个名为SwingDemo的文件夹)。在SwingDemo中有我的java文件以及资源文件)…

* src

  *SwingDemo
      *CustomDialog.java
      *Test.properties

当我在eclipse上运行这个时,一切都运行良好。但是,只要我试图从cmd行运行应用程序空指针异常正在发生..

命令行部署层次结构如下:

文件夹:D:WorkJava programs SwingDemo

层次:* SwingDemo

              *CustomDialog.java            
              *Test.properties

首先,我从cmd行(javac CustomDialog.java)编译了SwingDemo文件夹内的这个文件。然后我移动一步回到Java程序文件夹(正如我提到的。Java类中的包),并使用著名的"Java SwingDemo.CustomDialog"运行应用程序。当我以前使用new FileInputStream("path")时,我曾经遵循类似的步骤。在这样做之后,我得到空指针异常..

我认为"getClass(). getresource (url)"不能从特定目录加载文件。这就是为什么我把资源放在与我的java文件相同的目录中。它在Eclipse中运行良好。但是当我从命令行运行时,为什么会出现错误呢?

最新更新