Issue with external application.properties on Windows - java

I have a spring-boot jar file that I run on both Windows and Linux. Now, I want to pass in a custom application.properties file for this.
This works perfectly fine on Linux, but on Windows, for some reason, it does not pick up the external application.properties file even though it is in the same folder as the jar file. (I know this because there is a server that I have configured to something in the jar itself, but in the external properties file, I am pointing to a server running on localhost. When I run the jar it is trying to connect to the server configured in the jar)
I then tried passing a custom config file. Did not work. Based on some suggestion in some forum, I passed the external app properties as:
java -jar myApp.jar --spring.config.location=file:///c/jar-path/application.properties.
Now, it looks like it is picking up this properties file. But it still gives me an error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'my.config' in value "${my.config}
I have defined this property as:
#Value("${my.config}") String myConfig
I have added #Autowired to the class.
And in the app properties file, it is defined as:
my.config=true
What is surprising is that this works perfectly fine on Linux. Windows gives me the above error though. When I run this app from Eclipse on Windows, it runs perfectly fine too. But I see this error when run as a jar file. (Again only on windows not on Linux)
Any suggestions on what the issue could be? Is there anything different to be done for Windows platform vs Linux?
Thanks in advance...
Om

Related

Unexpected classpath behaviour remote deployment of Java application with Netbeans 13

Can anyone help me understand the classpath logic when deploying Java applications to remote hosts?
Netbeans will build, deploy and execute my Java application correctly on a remote Linux (Ubutntu 20.x) host.
Lets say that the executable JAR is deployed and executed in:
/home/user/project/dist
With any supporting library files copied to:
/home/user/project/dist/lib
This all makes sense to me.
However, I wish to read application and log4j2 configuration files. I would think that these should be placed in the same directory as the jar file. However they must be placed in the parent directory:
/home/user/project
Okay... BUT...
If I amend my code to write out a text file (to the executable directory) the resulting text file is written to:
/home/user/project/
<Edited 19Jun22>
I want my deployed application to read the log4j2.xml and configuration files from the same directory that my application writes to. What configuration settings must I change in Netbeans?
Alternatively is there a way to make Netbeans deploy to /home/user/project instead of /home/user/project/dist?

Springboot application not using the correct .properties file

I have a simple Springboot app that can be ran with the command: ./mvnw spring-boot:run. This works fine if I put the URI to my database inside of the application.properties file but the problem is that this file is used by Heroku, and is not meant for my local use.
So I came across a Stackoverflow answer that said I could simply make another .properties file but name it application-dev.properties and then when I run my app, the correct .properties file will automatically be chosen when I set the active profile to dev.
So I tried this by doing the following:
Make the application.properties file use the environment variable from Heroku since this is the .properties file I do NOT want to use locally.
I created a .properties file called application-dev.properties that has this line in it:
spring.data.mongodb.uri=mongodb+srv://MY_NAME:MY_PASSWORD#springbootcluster.v1maw.mongodb.net/Employees?retryWrites=true&w=majority
I run the app like this: ./mvnw spring-boot:run -Dspring.profiles.active=dev
The app fails with a ton of different errors because it is trying to use the application.properties file and not the application-dev.properties file
Part of the error message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';
ERROR MESSAGES
-Dspring.profiles.active is setting the spring.profiles.active system property in the JVM that's running Maven, not the JVM that's running your application. To fix the problem, use the spring-boot.run.jvmArguments system property to configure the arguments of the JVM that is used to run your application:
./mwnw -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
Alternatively, there's a property specifically for setting the active profiles which is slightly more concise:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
You can learn more in the relevant section of the reference documentation for Spring Boot's Maven plugin.

Zookeeper cli not starting

I've installed zookeeper v 3.4.9 on suse using the following package:
http://download.opensuse.org/repositories/home:/nicolasbock:/midokura/openSUSE_Leap_42.2/x86_64/zookeeper-3.4.9-1.1.x86_64.rpm
Starting the server works just fine an the various application that need to connect to it seem to be able to do so just fine, however I'm getting a strange error when I try to start the client:
zkCli.sh -server 127.0.0.1:2181
Error: Could not find or load main class org.apache.zookeeper.ZooKeeperMain
I've tried searching around the internet but the only answer I've come up with is setting the env variable $CLASSPATH. I tried looking what classpath is used internally by the serve starting script and set $CLASSPATH to that but with no results.
I'm running openjvm v 1.8.0_121
I encountered this problem today on Ubuntu 18.04. On zookeper official download page once one follows the given mirror link and chooses stable version, he can see 2 files available. In my case those were:
apache-zookeeper-3.5.5-bin.tar.gz
apache-zookeeper-3.5.5.tar.gz
I chose apache-zookeeper-3.5.5.tar.gz, observed the same problem. Tried to print CLASSPATH as #Jonathan suggested, it was printing different paths however, problem is that the archive file did not contain zookeper jars, probably it is expected that user packages to jar himself as I could find source code in that archive file.
Downloading larger apache-zookeeper-3.5.5-bin.tar.gz archive, extracting and running ./zkCli.sh went smooth as it had everything necessary, so whoever faces the same problem, try that and see if it does the trick for you.
Are you trying on Unix or Windows?
I dont think, you need "-server 127.0.0.1:2181"
Just run zkCli.sh or zkCli.cmd
That is a classpath problem. The zkCli.sh script is attempting to launch a java app where the main class is ZooKeeperMain. However, java cannot find the ZooKeeperMain class file because the classpath being passed to java does not contain a jar file that contains the ZooKeeperMain class.
Typically, the ZooKeeperMain.class file is contained in a jar file at something like $ZOOKEEPER_HOME/zookeeper-3.4.13.jar.
You might trying modifying the zkCli.sh script to print out the classpath just before the script attempts to launch ZooKeeperMain. The classpath should contain the jar that contains the ZooKeeperMain class.
On my Windows 10 machine, my classpath looks like this:
/c/Program Files (x86)/Zookeeper/bin/../build/classes:/c/Program Files (x86)/Zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/netty-3.10.6.Final.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/log4j-1.2.17.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/jline-0.9.94.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/c/Program Files (x86)/Zookeeper/bin/../zookeeper-3.4.12.jar:/c/Program Files (x86)/Zookeeper/bin/../conf:
I encountered a similar errors as yours when launching zkCli.sh from Git Bash for Windows on my Windows 10 machine. My issue was because of the way Git Bash was handling wildcards in the classpath. I initially had some *.jar entries in my classpath that I removed to get zkCli.sh to launch.

how to identify java class path and name to trouble shoot java

I'm using a java bootstrapping command to update a web application according to an xml file I've configured. The xml file has been checked and it's formatted correctly and referenced correctly in the script.
The problem: I keep getting an error that the system could not find or load the main class.
What I would like to do is to view the contents of the jar file. I know that to do this, I should run:
jar tf jarfilenameandpath
What I don't know is WHERE I should run the command. I tried it from a windows command prompt and received the following error:
'jar' is not recognized as an internal or external command, operable file, or batch file.
You don't have to run that command to see whats there in .jar better
way to do it is download the Java Decompiler
See all the classes and try to find the conflict. If everythings looks fine. The only way you can solve this problem is by debugging it.
Did you set Java PATH variable on your Windows machine?
For more details on de-compilers, visit this page on stackoverflow

Hibernate configuration file and Eclipse project export

I have a 3 projects under Eclipse : 1-CLIENT, 2-COMMON, 3-SERVER. The server project contains everything related to database managment (i.e. DAO...). I'm using hibernate and annotation for this project to access database. The common project contains also some objects commonly used by both the Server and the Client. Some of these objects are from the database. My Server project contains as well the HibernateUtils class that load the configuration file "hibernate.cfg.xml".
When I run the project under Eclipse, no problem, everything is going smoothly. Now comes the deployment... I'm exporting the server as a runnable jar file under Eclipse and the common project as a lib file. in the server jar file, I have the file "hibernate.cfg.xml".
When I launch the program, I'm ending up with an exception
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
I've tried to force the filename in the configuration process using new AnnotationConfiguration().configure("XXXX/hibernate.cfg.xml"), but nothing is working !
Even when I copy the debug command line used to launch the program and paste it under a command dos window, it's not working...
What am I missing there ?
Hibernate searches for its configuration in places addressed in CLASS_PATH. i suggest you put your hibernate.cfg.xml in your source folder. this often prevent likely problems.
Another suggestion! don not export your project using eclipse export wizard, try to create an ant file to do so, or maven if you can...

Categories