I'm trying to configure my Spring application.
I have in /src/main/resources my file application.yml. In my main class there is no #PropertySource annotation (but I tried also using that).
I also have another file on my Windows (/c/Programs/application.yml) that has similar content (some values are overriden).
I have to run application with following cmd call (my organization doesn't want me to use another call, because it part of CI/CD standard process):
java -Dspring.config.location=classpath:/application.yml,/c/Programs/application.yml -jar app.jar
My /src/main/resurces/application.yml file is included with .jar after building app. Running app with cmd above uses keys from resources, not from second file.
As I gathered from few hours of trying to solve this:
with two files spring.config.location, second one overrides values from first one,
files should be separated by comma,
I don't have to use spring.config.name, when I specify single file (not directory).
What is the problem with my configuration? How can I override file from resources with external file?
Related
I have written a small application to parse a large XML file using SAX with Intellij.
I pass -DentityExpansionLimit=0 option to my application by going to Run\Edit Configurations... and set VM options.
It works perfectly when I run the application with Intellij, but when I create the artifact with intellij it doesn't work and I get the error which needed to set that option. This is obvious that the option didn't pass to the created jar file.
How should I achieve this goal?
Is there any command that I create with a batch file or something to set this option for my user? Is there any setting file that I can modify to set this option for my machine? (I use windows 10)
Usually, to send system properties to a jar, the command is something like that:
java -DentityExpansionLimit=0 -jar thejar.jar
You are mixing up two things here:
the JVM command line command, and the fact that you can pass arguments to your application, or properties to the JVM itself
your deployment artefact (probably a JAR file)
Meaning: It seems like you want to either pass command line arguments (to some main function) or properties to your application. But the JAR file doesn't have support for that.
JAR files are just a container of class files. You can add some META information via the manifest (which class to run), but that is about it. You can't magically push your IntelliJ "runtime configuration settings" into the JAR.
In other words: IntelliJ has no way of putting these values into your JAR.
When you invoke java -jar Your.jar ... then you (or some other tooling) has to add the required values to the command line.
I have built a jar file which has a log4j.properties file in it (mvn package put it there by default from the resources directory). But when I run this jar file, I want to pass a different logging config, so I add -Dlog4j.configuration=file:{path to file}. The issue that bugs me is that the order matters here as follows:
When I run java -jar {path to jar} -Dlog4j.configuration=file:{path to file} then it reads the log file packaged in the jar.
When I run java -Dlog4j.configuration=file:{path to file} -jar {path to jar}, then it reads the config from the file I pass in the parameters.
I have rough understanding how classpaths work in java and that if I were to load several java classes with the same name, it would make a difference. But this way I am passing a config parameter with a -D prefix, so the way I expect this to work is for some code in log4j library to check whether -Dlog4j.configuration is set and if so, then load the config from there, otherwise try to find it on the classpath.
Any ideas on what I am missing?
If you provide anything after naming the JAR file, it is treated as an argument to your main method. For Log4J you actually have to define a property, and this needs to be done before you specify -jar.
I have written a java program
generates lots of files (say txt files) in different directories
and then reads the files and operates on them
I have exported the project as a runnable jar.
I need to run the jar on a remote server, obviously PATHs are not the same
What options do I have
Change path locations
Or is there another way out ?
Additionally I use different external programs that generate more files. And my program needs to read these files too.
Well it sounds like you need to be able to configure what your program does. How you do that is up to you. For example:
You could use command line arguments
You could use a configuration file loaded from a well-known location (or possibly loaded from the directory containing the jar file)
You could mix the two, specifying the configuration file location on the command line
The last option is probably the most flexible. Read the config file, which then contains all the other file locations you need. Each deployment can have a different config file.
I have a java desktop app (main project) and another project with a series of packages in NetBeans. Some of the packages use spring for JDBC and IOC.
I am getting the following error when running in debug:
Caused by: java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
Where is the config file supposed to go? Where exactly is the class path? Is it in dist, build, in the root of the project that calls spring, or the main project (the desktop app)?
confused ..
Your classpath is defined when you run your app using the java command. You can specify it using:
java -cp $path my.Main
where $path is your classpath. It is a :-separated (; on windows) list of JAR files and/or directories containing compiled .class files.
If you run your program like:
java -cp configdir my.Main
And put your spring config in configdir (the fully-qualified path) then that should be discovered.
NetBeans: whilst I'm not a netbeans user, it probably offers a number of ways for you to complete the task you want:
In your run configuration (i.e. where you define what class is being run, what the command-line parameters are etc), you will probably be able to add items to the classpath. These might be directories or individual files
In your compiler settings, you can probably tell NetBeans to automatically copy files of a certain type (like properties files, XML config files) from your source locations to where NetBeans puts your class files.
If you put your config.xml file in the directory where NetBeans is compiling your .class files to
Put it in the root folder of you application
if you created your application in a folder called Spring then you should put your file in that folder
Disregard the answer by oxbow_lakes. NetBeans modifies CLASSPATH, so what it is outside the IDE is no measure of what it is inside the IDE.
I've got a very small set of classes built up in a custom package hierarchy with one console app class that employs them. Everything works fine from JCreator and from a command prompt.
I'd like to build a second console app that re-uses that same package.
As a Java newbie, what is the quickest, dirtiest method to do that?
My main concern was avoiding copying the package directories over to the new console app's directory.
Using JCreator, I didn't have any problems adding the package directory to the project and compiling and running. But when I tried to run the console app from the command line, it couldn't find the classes in the package hierarchy.
In Visual Studio, you just add a reference...
What you want to do for both apps is create a jar file with a Main-class definition in the var manifest. There's a good bit of information on this in the Java Tutorials, but the gist of it is just that you'll create a jar file with the jar tool, and then make a little wrapper to run it as
java -jar myfile.jar
If you do not wish to copy your class files from your first application, then you need to set up the classpath used when you run java from the command line to include the location of those files.
Make sure you also include the location of your newly created class files.
Using a tool like process exlporer, you should be able to see the command line which JCreator used to launch your application.
(Typically IDEs also include lots of command line parameters for being able to connect to your application and debug it, etc.)
If I read this correctly, you have a class I'll call A that references packages in package pkg and now you wish to create a new class B that also uses the classes in pkg.
One option is to create a pkg.jar file that contains all the classes in pkg and then create separate jar files to hold A and B. In the manifest files for A.jar and B.jar you can include a Class-Path element to include pkg.jar
Then so long as pkg.jar is sent along with A.jar or B.jar, they would each reference the pkg.jar without having to worry about specifying the classpath on the command line.
Some details here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html