In projects, some functions are often done independently Java Project, And then hit jar Packages are available for other projects . If jar The configuration file information needs to be read in the package , The configuration is rarely typed into jar package , Because it's not easy to modify , It's more about jar The package reads the external configuration file .

properties The configuration file is removed from the project , Let's put it under engineering first 、 And src Parallel path . Pictured

Method 1:

become involved jar Packet time , Remove... First config

package com.main.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
* Read properties File tool class
*
* */
public class Tools {
private static Properties p = new Properties();
static {
try {
// System.getProperty("user.dir") Get the absolute path of the project , Then assemble the path of the configuration file
// Read out of system configuration file ( namely Jar Out of package files ) --- External engineering refers to the Jar The package needs to be created under the project config The directory holds the configuration file
String filePath = System.getProperty("user.dir") + "/config/data.properties";
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
p.load(in);
} catch (IOException e) {
System.out.println(" Error reading configuration information !");
}
}
/**
* according to key obtain value Value
*/
public static String getValue(String key) {
return p.getProperty(key);
}
}
 

java The packaging project puts the configuration file outside the package ( There will be more in the future ) More articles about

  1. idea Medium java web project ( add to jar Package introduction ) and java maven web Project directory structure

    java web project web Under the project web The root directory name can be changed idea New China java web project , Default src by Sources Root, Of course, it can also be changed manually , stay Sources Root Right click to create a new one Pa ...

  2. It's downloaded from the Internet java Open source project how hit jar package

    At present a lot of java Open source project ( for example qlexpress) Only the source code is provided , Not provided jar, Here is maven hit jar How to pack . 1. Get into qlexpress The directory where the source code is downloaded , This directory should contain pom.xml, as follows ...

  3. IntellJ idea maven pack —— The project has a third party jar package

    Use today maven When the packaging , Prompt package com.aspose.slides non-existent It turns out that the third party jar It's imported directly , I didn't find it when I packed it There's a third party in that project jar package , How to pack ? Method : 1. Root path establishment lib package , Will the third party ...

  4. 【IneliJ 】 Use IneliJ IDEA 2016 take Java Web The project is exported as War package

    This document records the use of IDEA export war The process of the package and the solution to the problem Although it's changed to IDEA Developed , But it still works eclipse hit war package …. Embarrassed It's not the way to go on like this ... So try to use it today IDEA package . term ...

  5. Use IneliJ IDEA 2016 take Java Web The project is exported as War package

    This document records the use of IDEA export war The process of the package and the solution to the problem Although it's changed to IDEA Developed , But it still works eclipse hit war package -. Embarrassed It's not the way to go on like this ... So try to use it today IDEA package . term ...

  6. java Read xml( When xml When it's in the bag )

  7. JAVA Web Project acquisition src and WebContent The configuration file under the directory

    One , obtain src The following profile information 1, Structure diagram is as follows : package com.binp.properties; import java.io.FileInputStream; import java.i ...

  8. Aliyun deployment Java web The first experience of the project

    Lin Bingwen Evankaka Original works . Reprint please indicate the source http://blog.csdn.net/evankaka Abstract : This article mainly talks about how to install JDK.Tomcat And its configuration process . Finally, an example is given to show ...

  9. Aliyun deployment Java web The first experience of the project ( turn )

    Lin Bingwen Evankaka Original works . Reprint please indicate the source http://blog.csdn.net/evankaka Abstract : This article mainly talks about how to install JDK.Tomcat And its configuration process . Finally, an example is given to demonstrate the application of ...

Random recommendation

  1. Eclipse The configuration javaweb Projects are rapidly deployed to tomcat

    Used to VS, Reuse Eclipse, There is a feeling of shifting from automatic to manual . Many students are in Eclipse Lower development web project , Every time you change the code , When looking at the effect, there are right-click items ->Run as -> Run on ...

  2. 60 That's ok JavaScript tetris

    <!doctype html><html><head></head><body> <div id="box" st ...

  3. Bouncy Castle out of memory

    The phenomenon : Heap memory overflow ,java.lang.OutOfMemoryError: Java heap space use jmap see , Show num     #instances         #bytes   ...

  4. use Git Export project

    Git No, SVN The export function of , Can not be like svn export url like that , Export a version of code to a folder without a version control file .   Git Provides archive command , You can export the version file stream .   Can be Git ...

  5. PowerShell_ Zero foundation self study course _6_PS Get help information from 、 The Conduit 、 Format input

    Previous articles have talked about these topics one after another , But the discussion is not deep enough , Today, let's take a closer look at getting help . Pipeline and formatted output . One . Get help stay PS For help in , The most commonly used ones are : -? .get-comm ...

  6. Overview of soft testing ——PV operation

     In the operating system , There is often mutual exclusion between processes ( When you need to share exclusive resources ) And synchronization ( Finish the cooperation of two asynchronous processes ) Two relationships . And semaphores and PV The operation perfectly and effectively deals with these two situations .     Mutually exclusive : It's like crossing a single wooden bridge , One ...

  7. eclipse Custom code template

    eclipse Custom code template Eclipse There are a lot of code templates available , We can go through Windows->Preferences->Java->Editor->Templates ...

  8. Numpy

    One  : install ipython And the package used # Here we will use ipython Interpreter , The code in this article ipython Run under Pip3 install ipython Pip3 install jupyter ...

  9. 【linux Users of , password , Group management 】

    One . User and password The user account The super user :UID=0 root Ordinary users :UID!=0 System users : 0<UID<500 In order to maintain some functions of the system or realize some services Unable to complete login authentication General use ...

  10. Git Common commands ( One )------ Local operation

    This article summarizes from Liao Xuefeng's website . A few nouns : work area (Working Directory): The directory you can see on your computer Version Library (Repository): Contains the staging area and master Temporary storage area (Stage): To be put in maste ...