Config file read in java

How to read a configuration file in Java

How can I make configuration file for the above task ? Output: Solution 2: Create a configuration file and put your entries there.

How to read a configuration file in Java

I am doing a project to build thread pooled web server , in which I have to set

  • the port number on which server listens.
  • How many threads are there in thread pool
  • Absolute Path of the root directory, and so many points.

One way is to hard code all these variables in the code, that I did. But professionally it is not good.

Now, I want to make one configuration file, in which I put all these data, and at the run time my code fetches these.

How can I make configuration file for the above task ?

app.name=Properties Sample Code app.version=1.09 

Source code:

Properties prop = new Properties(); String fileName = "app.config"; try (FileInputStream fis = new FileInputStream(fileName)) < prop.load(fis); >catch (FileNotFoundException ex) < . // FileNotFoundException catch is optional and can be collapsed >catch (IOException ex) < . >System.out.println(prop.getProperty("app.name")); System.out.println(prop.getProperty("app.version")); 
Properties Sample Code 1.09 

Create a configuration file and put your entries there.

SERVER_PORT=10000 THREAD_POOL_COUNT=3 ROOT_DIR=/home/ 

You can load this file using Properties.load(fileName) and retrieved values you get(key) ;

Start with Basic I/O, take a look at Properties, take a look at Preferences API and maybe even Java API for XML Processing and Java Architecture for XML Binding

And if none of those meet your particular needs, you could even look at using some kind of Database

Java.lang.NoClassDefFoundError: org/hibernate/cfg, In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to …

Java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

I’m trying to do an initial setup with Hibernate in Eclipse, while deploying with Tomcat.

I encountered the following problem:

java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) db.HibernateUtil.buildSessionFactory(HibernateUtil.java:13) db.HibernateUtil.(HibernateUtil.java:8) 

I’ve goggled my problem online and I understand that it has something to to with the project class path.

I’ve added a user library to my project containing all the jars under the «required» directory in the hibernate download, but that doesn’t seem to work.

Please find a screen shot of my project here:

You’re getting the error because the hibernate libraries are not available to Tomcat. In your picture, below the hibernate library set there is an empty set called ‘Web App Libraries’ — this is the set your hibernate libraries need to be in.

Right click your project -> Build Path -> Configure build path, and remove the hibernate set from the build path. Now import the jars into the WEB-INF/lib folder. Refresh your project and now you should see them listed in the ‘Web App Libraries’ set (i appreciate this is somewhat annoying that you have to import them into your code base — someone else might know a better way to do this that doesn’t involve copying the jars in)

This error can happen when your jar files has not been exported with your web archive. You can do what @Chris White has said or you can follow below steps if you are using Eclipse.

Right click the project -> Build Path -> Configure Build Path

In the list at left side click on Deployment Assembly . Click » Add » button. Select » Java Build Path Entries » and click next . Now select your hibernate jar files or library. Click finish . Then click Ok .
Restart the server if necessary.

Simpler way than Chris White’s way.

You’re getting the error because the hibernate libraries are not available to Tomcat. Right click your project -> Build Path -> Configure Build Path, and remove the hibernate set from the build path.

Do the same stuff, but instead of importing the jars everytime for a new project, **copy the required hibernate jars in lib folder of Apache Tomcat **(or whatever the server u are using).

You don’t have the hibernate jar present in your library folder Visit the below link for the possible solution, http://www.smashplus.info/2012/11/javalangnoclassdeffounderror.html

Java — Setting active profile and config location from, The other 3 profile specific config files are present in C:\config folder. I am using gradle plugin for eclipse. When I try to do a «bootRun», I am …

Java -jar : access external configuration file

I’m looking to do something which I thought was not going to be difficult.

I have an application that I’d like to package up as a jar because I’ve got ~30 dependencies and I would like to be able to deploy a single file.

I have some configuration files — a properties file and a spring configuration file, and my log4 props file — that I would like to have external to the jar. I guess I expected that if I put them in the same directory as the jar it would find them when it ran, but it doesn’t.

While developing, I have these files at the root of the classpath for my eclipse project and the app finds them just fine. I feel like I’m missing some key aspect of jar / classpath theory.

so what I want is to be able to put the config files and the jar in the same directory and have the app find the config files when I run it with the standard java -jar thing.

Is there not a simple way to achieve this?

I will preserve for posterity my solution to this problem.

I think it’s possible that I was expecting java -jar to do something it doesn’t do.

If you use the regular java command you can include the jar on the classpath and you’ll end up with pretty much the same thing as java -jar, only you have to specifically name the class you want to use. It looks like this:

java -cp .:path/to/Jar.jar com.company.package.Class arg1=val1 arg2=val2 . 

Chances are you’re going to build a script to start the program for you anyway, so the added complexity of the command line call doesn’t really amount to much, since you’ll only build it once anyway.

You’ll see I included ‘.’ as the first item on the classpath, which means anything in the same directory as the jar will be included on the classpath as well. Of course everything inside the jar is included on the classpath also, since the jar itself is on the classpath too.

You’ll also see that I’ve shown how you can still hand in args on the command line. This was something I was unsure about when I started but it works as expected.

I’m sure this is just basic java deployment knowledge, but I was lacking it for some reason.

I hope this helps someone else. It certainly would have saved me a lot of time if someone had been able to say «don’t bother trying to get the -jar thing working — just use the -cp method».

You need to add «.» to the classpath of the jar file you are building for your application.

So in the manifest, I’d expect to see

Main-Class: some.full.Name Class-Path: . needed-lib.jar other-lib.jar 

Then, when you run your app by executing

java -classpath .;needed-lib.jar;other-lib.jar some.full.Name 

This way any file in the directory with the myapp.jar file will also be on the classpath. That classpath is relative to the location of jar containing it.

Log4j for one expects the log4j.xml configuration file to be on the classpath. If you aren’t using the name log4j.xml you also have to add a system property to your start up command to tell the log4j library to look for a different name.

I’m not sure what Spring expects for configuration files. And property file loading depends on what mechanism is used to load the file. Using a FileReader or InputStream doesn’t use the classpath mechanism at all. In that case you need to know where the application is expecting the file to be relative to the current working directory.

Properties — Config file for a Java Application, This allows configuration of arbitrary complexity without additional coding, since you’re storing Java data directly. It takes a bit of work …

Config file for a Java Application

I am building a Java Application with the intention of packaging it to be a windows executable in the future. My NetBeans project structure can be seen here. I am currently reading from config.properties using getResourceAsStream(). I was not aware that this file cannot be written to in a similar manner. Would anyone be able to advise a way to achieve this or is a different approach to my problem required? Thanks.

I think you can use java.util.properties to solve your problem; to write to your properties first create a new Properties object, load them via your InputStream , use methods such as setProperty to add to your configuration and finally use store to write to them.

File appConfig = new File("a_file"); FileInputStream propsInput = new FileInputStream("a_file"); Properties prop = new Properties(); prop.load(propsInput); try (Writer inputStream = new FileWriter(appConfig)) < // Setting the properties. prop.setProperty("A_PROP", "A_VALUE"); // Storing the properties in the file with a heading comment. prop.store(inputStream, "INFORMATION. "); >catch (IOException ex)

There’s no straightforward answer to the question how to externalize configuration — it depends on the application and the environment in which it will be deployed.

The Java preferences API is an attempt to get around this problem by providing a platform-neutral API for preferences. On Linux, the data is actually stored in an XML file in a subdirectory of $HOME/.java but, of course, the programmer isn’t supposed to be concerned about this. Using this API is convenient, but doesn’t allow complex data to be stored, and doesn’t easily allow the programmer to provide the application with a way to read configuration from some specific place.

Rather than use the preferences API you can read/write files explicitly. For simple ‘name=value’ configuration, you can use a Properties object, which has methods for handling files. This method does allow a specific file location but, of course, controlling the file location does require some adaptation for the various platforms you might support.

If your configuration is more complex than ‘name=value’, you could read/write it in a format of your choice — JSON, YAML, XML, etc. All these file formats are well-supported by Java. Using a text-based format allows users to edit the configuration using other tools, if they so wish.

In practice, I nearly always externalize configuration in serialized Java objects, into a file whose location I control. This allows configuration of arbitrary complexity without additional coding, since you’re storing Java data directly. It takes a bit of work to structure a program to be serialized, but it’s worth it in the long run. Unfortunately, this approach does not allow for easy editing of the configuration outside the program.

If you ever intend to run the application in a container (Docker, etc), then completely different considerations apply. You’ll need to use a method of externalizing configuration that is compatible with the container platform — simply reading and writing files is often inappropriate in such an environment.

If you’re unsure how you want to handle configuration, do it in a class that implements an interface you define. Then, if you later change your mind, you can supply a different class that implements the same interface.

Unable to start Eclipse, Problem: Eclipse Neon error: «C:\Program Files\Java\jre1.8.0xx.xx\jvm.cfg». Solution Win7 Pro: System Properties > Enviorment Variables > System …

Источник

Читайте также:  Лучшие фреймворки для css
Оцените статью