标签: properties-file

由于.properties文件加载,可执行.jar无法启动

我有运行可执行.jar文件的问题.我创建了一个包含.properties文件的项目.当我从eclipse启动它时,它工作得很好,但是当我将它导出到可执行的.jar文件并尝试运行它时:

java -jar myfile.jar
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

(无法在此处发布图片) http://imageshack.us/photo/my-images/824/29583616.png/

我已经检查了.jar中的清单文件,它包含了

Class-Path: .
Run Code Online (Sandbox Code Playgroud)

这是属性文件加载:

properties = new Properties();
    properties.load(new FileInputStream(
            "src/com/resources/treeView.properties"));
Run Code Online (Sandbox Code Playgroud)

知道导致这个异常的原因吗?

java jar executable-jar properties-file

4
推荐指数
1
解决办法
3823
查看次数

使用java从属性文件中删除键和值

  • 我的属性文件中有一些插入的值.会是这样的,

键=值

  • 我必须更新属性文件.在更新时,检查密钥是否可用.如果有密钥,我需要删除密钥和值,并且必须再次写入.
  • 在更新/再次写入之前,任何人都可以给我删除现有密钥和值的代码.

这是我要插入和更新的java代码:

 if (action.equals("insert")) {
  if (con != null) {
    if (key == null) {
      //session.setAttribute(username, con); 
      out.println("****Connected Successfully****");
      String rootPath=request.getSession().getServletContext().getRealPath("/");
      System.out.println(rootPath);
      String propPath=rootPath+"/WEB-INF/";
      PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true)));
      out1.println(cname+"=jdbc:oracle:thin:@"+host+":"+port+"/"+service+","+username+","+password);
      out1.close();
    } else {         
      out.println("*Connection name "+cname+" already exists. Please try with another name");
    }
  }
}
if (action.equals("update")) {
  if (con != null) {
    if (key == null) {
      out.println(cname+" is not available.");
    } else {
      String rootPath=request.getSession().getServletContext().getRealPath("/");
      System.out.println(rootPath);
      String propPath=rootPath+"/WEB-INF/";
      PrintWriter out1 …
Run Code Online (Sandbox Code Playgroud)

java properties properties-file

4
推荐指数
1
解决办法
9495
查看次数

使用自定义属性文件

我希望有一个与我的应用程序一起部署的属性文件,以便我可以在运行时访问它.如何让我的build.gradle文件访问我的属性文件src/main/resources

我想像我正常使用的那样使用这个文件gradle.properties.

gradle properties-file

4
推荐指数
2
解决办法
7800
查看次数

将数据附加到属性文件,注释消失并且数据顺序发生变化

在将数据附加到属性文件时,现有注释将消失,并且数据顺序正在更改.请建议如何避免它?

属性文件中的数据(在附加数据之前)和注释如下:

# Setting the following parameters 
# Set URL to test the scripts against
App.URL = https://www.gmail.com
# Enter username and password values for the above Test URL
App.Username = XXXX
App.Password = XXXX
Run Code Online (Sandbox Code Playgroud)

我正在向上面的属性文件添加更多数据,如下所示:

 public void WritePropertiesFile(String key, String data) throws Exception
{       
    try 
    {
        loadProperties();  
        configProperty.setProperty(key, data);
        File file = new File("D:\\Helper.properties");
        FileOutputStream fileOut = new FileOutputStream(file);
        configProperty.store(fileOut, null);
        fileOut.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

将上述功能称为:

help.WritePropertiesFile("appwrite1","write1");
help.WritePropertiesFile("appwrite2","write2");
help.WritePropertiesFile("appwrite3","write3");
Run Code Online (Sandbox Code Playgroud)

数据添加成功,但先前输入的注释消失,数据顺序也发生变化,属性文件(追加数据后)显示如下

#Tue Jul 02 11:04:29 …
Run Code Online (Sandbox Code Playgroud)

java properties-file

4
推荐指数
1
解决办法
7633
查看次数

读取具有特定字符串的java中的属性文件

我正在使用一个.properties文件.在那我有以下配置参数:

Appnameweb = app1
Appnamemobile = app2
Appnameweb1 = app3
Run Code Online (Sandbox Code Playgroud)

可以有许多以用户提供的Appname开头的配置参数.如何读取所有属性文件参数,其中键将包含特定的字符串,如本例中的Appname?

java properties-file

4
推荐指数
1
解决办法
2万
查看次数

java.io.FileNotFoundException:无法打开类路径资源[WEB-INF/classes/library.properties],因为它不存在

在我的Spring应用程序中,我有一个位于文件夹中的简单属性文件,WEB-INF\classes以便它,DispatcherServlet以及各种其他配置文件classpath.

props文件在DispatcherServletas中定义:

<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">            
           <value>/WEB-INF/classes/library.properties</value>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

propertiesFactorybean注入控制器:

@Autowired 
private Properties propertiesFactory;
Run Code Online (Sandbox Code Playgroud)

并在控制器的一种方法中使用:

if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {           
Run Code Online (Sandbox Code Playgroud)

这一切都完美无缺,除了以下测试程序有以下几行:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml");
Run Code Online (Sandbox Code Playgroud)

哪一个抛出BeanCreationException:

Injection of autowired dependencies failed
Run Code Online (Sandbox Code Playgroud)

因为:

java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist
Run Code Online (Sandbox Code Playgroud)

但是如果整个应用程序都可以看到props文件,为什么不用这个程序呢?

java spring dependency-injection properties-file

4
推荐指数
1
解决办法
4万
查看次数

在Java Jersey RESTful Web应用程序中加载属性文件,以在整个应用程序中保留?

我目前正在使用Jersey构建RESTful API.到目前为止,一切进展顺利,但是,所有配置条目都已经过硬编码.(即数据库主机,数据库用户名等).

我希望能够设置config.properties我的WEB-INF文件夹中存在的文件,以包含所有这些配置规范.

我担心如果我在Classpath上读取文件的"经典"方式,我会为每个请求执行文件I/O. 我希望能够在启动时读取一次(我知道这涉及到ServletListener我的web.xml文件中.

这是我在下面的内容:

web.xml:

<listener>
    <listener-class>com._1834Software.Config</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

我想做类似这样的事情(我在StackOverflow上找到了这个),但我认为它不一定适用于Jersey:

Config.java

public class Config implements ServletContextListener {
    private static final String ATTRIBUTE_NAME = "config";
    private Properties config = new Properties();

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {

            config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));

        } catch (IOException err) {
            err.printStackTrace();
        }

        event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);

    }

    @Override
    public void contextDestroyed(ServletContextEvent event) { /**/ }

    public static Config getInstance(ServletContext context) {
        return …
Run Code Online (Sandbox Code Playgroud)

java xml config jersey properties-file

4
推荐指数
1
解决办法
1万
查看次数

如何使用命令行输入来选择要在 gradle 构建中使用的属性文件?

我有一个 java 项目,可以根据我将为其构建的环境使用几个不同的属性文件之一(例如,development.properties 将为我提供适合开发环境的属性),并且我希望能够通过在命令行中选择它来控制构建中包含哪个 gradle。我知道您可以通过在 build.gradle 文件中添加类似的内容来选择要包含在构建中的属性文件:

Properties props = new Properties()
props.load(new FileInputStream("/path/file.properties"))
Run Code Online (Sandbox Code Playgroud)

但我希望能够在构建项目时即时选择要使用的属性文件。

java gradle properties-file

4
推荐指数
1
解决办法
2130
查看次数

Spring Boot集成测试不会读取属性文件

我想创建集成测试,其中Spring Boot将使用@Value注释从.properties文件中读取值.
但每次我运行测试时,我的断言都会失败,因为Spring无法读取值:

org.junit.ComparisonFailure: 
Expected :works!
Actual   :${test}
Run Code Online (Sandbox Code Playgroud)

我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebTests.ConfigurationClass.class, WebTests.ClassToTest.class})
public class WebTests {
    @Configuration
    @ActiveProfiles("test")
    static class ConfigurationClass {}

    @Component
    static class ClassToTest{
        @Value("${test}")
        private String test;
    }

    @Autowired
    private ClassToTest config;

    @Test
    public void testTransferService() {
        Assert.assertEquals(config.test, "works!");
    }
}
Run Code Online (Sandbox Code Playgroud)

src/main/resource包下的application-test.properties包含:

test=works! 
Run Code Online (Sandbox Code Playgroud)

这种行为的原因是什么?我该如何解决?
任何帮助高度赞赏.

java spring junit4 properties-file spring-boot

4
推荐指数
2
解决办法
5700
查看次数

IntelliJ:查看代码中的属性值

在较新版本的 IntelliJ 中,注释中使用的属性值@Value(也可能在其他上下文中)显示如下:

在此输入图像描述

如果您单击了预览或因为有时 IntelliJ 会忽略它,则此操作不起作用:

在此输入图像描述

如何强制 IntelliJ 显示属​​性值?

java spring intellij-idea code-folding properties-file

4
推荐指数
1
解决办法
1827
查看次数