简体   繁体   English

在Java中创建独立程序包并从配置文件中进行一些设置

[英]Creating a standalone package in java and take some settings from the configuration file

Anyone plz let us know what to do when we have some configuration file which is basically xml.I want to for example give the path to save the image(for my java program) in a folder from some config file (xml in my case).In that case where should the config file be kept.Rt now every thing is converted to jar file when i create a java standalone package.But i want to give some setting from xml file.What to do in that case.How is it possible.This article only provides to create a single jar file for java project but talks nothing about the configuration settings that u can provide from some external source. 任何人请让我们知道当我们有一些基本为xml的配置文件时该怎么办。例如,我想提供从某些配置文件(在我的情况下为xml)将图像(对于Java程序)保存在文件夹中的路径在那种情况下应该保留配置文件。现在我创建一个Java独立程序包时,所有东西都会转换为jar文件,但是我想从xml文件中进行一些设置。在这种情况下该怎么办。本文仅提供了为Java项目创建单个jar文件的内容,但没有提及您可以从某些外部来源提供的配置设置。

Regards Sagar 问候萨加尔

I'm not sure I fully understand your question, but if it is where to put the XML file with configuration information, you can place your xml file in the same directory as your jar file, and then pass the XML file name and path into the Jar on the command line when calling the Jar. 我不确定我是否完全理解您的问题,但是如果要将XML文件和配置信息放在哪里,则可以将XML文件放置在与jar文件相同的目录中,然后将XML文件名和路径传递到调用Jar时,在命令行上使用Jar。 If you're running it in Windows, this is often done using a shortcut. 如果您在Windows中运行它,通常可以使用快捷方式来完成。 Then you can get the full path string for the Jar from the main method's String[] arg array that accepts the command parameters. 然后,您可以从接受命令参数的主方法的String [] arg数组中获取Jar的完整路径字符串。

Sagar, 萨加尔

The fact your java program is a standalone package (.jar file) has no bearing on where your configuration file is stored. 您的Java程序是独立程序包(.jar文件)的事实与配置文件的存储位置无关。 Your java package is a program and that program can read any file from the file system that it so desires; 您的java包是一个程序,该程序可以从文件系统中读取所需的任何文件; it does not have to be part of the code inside the IDE ie you don't have to write it when you write the program. 它不必是IDE里面的代码的一部分,即你没有当你写程序写出来。 What you do need is some way, when you start the program, to find and read said configuration file. 您需要做的是在启动程序时以某种方式查找和读取所述配置文件。

Depending on how you expect the program to be configured, you might put that file in a number of locations. 根据您期望程序配置的方式,可以将该文件放置在多个位置。 For example, /etc/yourimageprogram/config.xml or c:\\program files\\yourimageprogram\\config.xml or perhaps c:\\users\\Sagar\\Application Settings\\yourimageprogram\\config.xml. 例如,/ etc / yourimageprogram / config.xml或c:\\ program files \\ yourimageprogram \\ config.xml或c:\\ users \\ Sagar \\ Application Settings \\ yourimageprogram \\ config.xml。 Which you choose of those options really depends on what the use case is and that I can't help with. 您从这些选项中选择哪种实际上取决于用例,而我对此无能为力。

However, there are some main points to reading any file: 但是,读取任何文件有一些要点:

  • Does it exist? 是否存在?
  • Are we allowed to open it for reading? 我们可以打开它阅读吗?
  • Are we allowed to open it for writing? 我们可以打开它进行写作吗? Might want to know if we want to update the config? 可能想知道我们是否要更新配置?

In Java, typically, you would test this with: 通常,在Java中,您可以使用以下方法进行测试:

File configfile = new File("C:\test.xml");
if ( configfile.exists() && configfile.canRead() )
{
    // read the file
}
else
{
    // decide what to do if no config exists.
    // might be first run of app.
}

The next stage is to parse the file. 下一步是解析文件。 There are a number of parsers available for XML including sax and org.w3c.dom. 有许多可用于XML的解析器,包括sax和org.w3c.dom。 What you need to do is to use these to extract the information you require and store that in a class. 您需要做的是使用这些信息提取所需的信息并将其存储在类中。 Probably a singleton class as you're unlikely to have multiple configuration instances per instance of the program. 可能是单例类,因为您不太可能在程序的每个实例中拥有多个配置实例。

I suggest you read about XML Parsers and File Handling under Java . 我建议您阅读Java下的 XML解析器文件处理 Also look at the File object. 还要看一下File对象。 See all your options for file io in java. 在Java中查看文件io的所有选项 These should give you some indication of how to proceed. 这些应该给您一些指示如何进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 JAR 文件创建独立的 Mac 应用程序 - Creating standalone Mac App from JAR file 如何将alfresco jlan文件服务器设置为独立的Java包? - How to setup alfresco jlan file server as a standalone java package? 如何从独立的* .java源文件生成包结构? - How to generate package structure from standalone *.java source files? IntelliJ 插件:以编程方式编辑运行配置设置和/或从上下文创建 - IntelliJ plugin: Editing Run Configuration settings programmatically and/or creating from context 从独立Java客户端以编程方式创建WebLogic用户 - Creating WebLogic users programmatically from a standalone Java client 在Java应用程序中创建包含安全性的配置文件 - Creating a configuration file in a Java application including security 具有自定义设置的配置文件? - Configuration file with custom settings? Java Jar文件的独立属性文件? 如何为不同的服务器设置配置? - Standalone properties file for Java Jar file? How to set up the configuration for different servers? 独立Java程序中的注释基础配置 - Annotation base configuration in standalone Java program 如何创建.INI文件以在Java中存储一些设置? - How to create an .INI file to store some settings in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM