I am working on Spring boot application. I run my application via a batch file using:
java -jar -Dspring.config.additional-location=file:/config/ -Dspring.profiles.active=profile myApplication.jar
pause
This is working fine. However, I want to delete Ojdbc6 jar entry from my Pom file and define it to my classpath via a batch file, like so:
SET CLASSPATH=%CLASSPATH%ojdbc6.jar;
echo %CLASSPATH%
java -jar -Dspring.config.additional-location=file:/config/ -Dspring.profiles.active=profile myApplication.jar --classpath=%CLASSPATH%
pause
This is not working and I'm getting an error:
Unable to create initial connections of pool.
java.sql.SQLException: Unable to load class: oracle.jdbc.OracleDriver from ClassLoader:org.springframework.boot.loader.LaunchedURLClassLoader#439f5b3d;ClassLoader:TomcatEmbeddedWebappClassLoader
context: application name
delegate: true
----------> Parent Classloader:
org.springframework.boot.loader.LaunchedURLClassLoader#439f5b3d
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:292)**
Use -classpath=%CLASSPATH% instead of --classpath=%CLASSPATH%
Ref. link: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html
Related
I am starting spring boot project as follows
java -classpath "/home/madhur/github/fantasy_fury/target/fantasy_fury.jar:/home/madhur/github/fantasy_fury/target/dependency/*:/home/madhur/github/fantasy_fury/conf" com.games24x7.fantasy.fury.FantasyFuryInit
The conf folder has an application.properties with following contnents
zkHost=${ZK_CLUSTER_URLS}
Now, I am trying to read this variable as follows:
#Value("${zkHost:localhost}")
String zkHost;
However, it does not contain the actual value, but the default value localhost value every time.
Tried to set it as follows:
export ZK_CLUSTER_URLS=madhur1;java -classpath "/home/madhur/github/fantasy_fury/target/fantasy_fury.jar:/home/madhur/github/fantasy_fury/target/dependency/*:/home/madhur/github/fantasy_fury/conf" com.games24x7.fantasy.fury.FantasyFuryInit
Then also, the output is localhost. What I am missing?
Spring boot version is 1.4
I'm creating a custom Dockerfile with extensions for official keycloak docker image. I want to change web-context and add some custom providers.
Here's my Dockerfile:
FROM jboss/keycloak:7.0.0
COPY startup-config.cli /opt/jboss/tools/cli/startup-config.cli
RUN /opt/jboss/keycloak/bin/jboss-cli.sh --connect --controller=localhost:9990 --file="/opt/jboss/tools/cli/startup-config.cli"
ENV KEYCLOAK_USER=admin
ENV KEYCLOAK_PASSWORD=admin
and startup-config.cli file:
/subsystem=keycloak-server/:write-attribute(name=web-context,value="keycloak/auth")
/subsystem=keycloak-server/:add(name=providers,value="module:module:x.y.z.some-custom-provider")
Bu unfortunately I receive such error:
The controller is not available at localhost:9990: java.net.ConnectException: WFLYPRT0053: Could not connect to remote+http://localhost:9990. The connection failed: WFLYPRT0053: Could not connect to remote+http://localhost:9990. The connection failed: Connection refused
The command '/bin/sh -c /opt/jboss/keycloak/bin/jboss-cli.sh --connect --controller=localhost:9990 --file="/opt/jboss/tools/cli/startup-config.cli"' returned a non-zero code: 1
Is it a matter of invalid localhost? How should I refer to the management API?
Edit: I also tried with ENTRYPOINT instead of RUN, but the same error occurred during container initialization.
You are trying to have Wildfly load your custom config file at build-time here. The trouble is, that the Wildfly server is not running while the Dockerfile is building.
Wildfly actually already has you covered regarding automatically loading custom config, there is built in support for what you want to do. You simply need to put your config file in a "magic location" inside the image.
You need to drop your config file here:
/opt/jboss/startup-scripts/
So that your Dockerfile looks like this:
FROM jboss/keycloak:7.0.0
COPY startup-config.cli /opt/jboss/startup-scripts/startup-config.cli
ENV KEYCLOAK_USER=admin
ENV KEYCLOAK_PASSWORD=admin
Excerpt from the keycloak documentation:
Adding custom script using Dockerfile
A custom script can be added by
creating your own Dockerfile:
FROM keycloak
COPY custom-scripts/ /opt/jboss/startup-scripts/
Now you can simply start the image, and the built features in keycloak (Wildfly feature really) will go look for a config in that spedific directory, and then attempt to load it up.
Edit from comment with final solution:
While the original answer solved the issue with being able to pass configuration to the server at all, an issue remained with the content of the script. The following error was received when starting the container:
=========================================================================
Executing cli script: /opt/jboss/startup-scripts/startup-config.cli
No connection to the controller.
=========================================================================
The issue turned out to be in the startup-config.cli script, where the jboss command embed-server was missing, needed to initiate a connection to the jboss instance. Also missing was the closing stop-embedded-server command. More about configuring jboss in this manner in the docs here: CHAPTER 8. EMBEDDING A SERVER FOR OFFLINE CONFIGURATION
The final script:
embed-server --std-out=echo
/subsystem=keycloak-server/theme=defaults/:write-attribute(name=cacheThemes,value=false)
/subsystem=keycloak-server/theme=defaults/:write-attribute(name=cacheTemplates,value=false)
stop-embedded-server
WildFly management interfaces are not available when building the Docker image. Your only option is to start the CLI in embedded mode as discussed here Running CLI commands in WildFly Dockerfile.
A more advanced approach consists in using the S2I installation scripts to trigger CLI commands.
In my Spring Boot configuration file, can I specify a logging file location relative to the Spring Boot jar instead of relative to the directory the jar is started from?
Example:
#application.yml
logging:
file: ./logs/app.log
Directories:
/app/dir/app.jar
/random/dir
Start:
cd /random/dir
java -jar /app/dir/app.jar
Result:
/random/dir/logs/app.log
Desired:
/app/dir/logs/app.log
Additional Info:
The jar will be running on different operating systems. So the jar path could be similar to:
/app/dir/app.jar
C:\app\dir\app.jar
At run time, I can calculate what the jar/install directory is. Is there a Java solution that would allow me to set the logging.file location after startup? Or is the logger already initialized at that point?
Complete Solution: based on #satyesht
#application.yml
app:
log:
#Set a default directory in case a user "double clicks" the jar to launch it.
dir: ./logs
file: app.log
logging:
file: ${app.log.dir}/${app.log.file}
Start:
cd /random/dir
java -Dapp.log.dir=/app/dir/logs -jar /app/dir/app.jar
One simple way would be to pass the log directory as a system property as below .
#application.yml
logging:
file: ${LOGDIR}/logs/app.log
and while running your application you can specify the LOGDIR system property as a VM argument
cd /random/dir
java -jar /app/dir/app.jar -DLOGDIR=/app/dir/
You would have to know the location of the jarfile ahead of time. But yes, the difference is between absolute, and relative paths.
Using . indicates that you want to start from the location where our console window is open. This is a path that is relative to the location you are in the terminal.
Using / indicates that you want to start at the root of the drive. Now, obviousally you don't want to store the file in the root of the drive. But let's say i Had my jar in the folder: /app/dir/app.jar Then configuration is as simple as saying:
#application.yml
logging:
file: /app/dir/logs/app.log
How to wrap Spring Boot application as a linux daemon and to set it to read from application.properties.
To start the jar with the parameters from the application.properties I am using this command:
java -Dspring.config.location=/application.properties -jar MyJar.jar
Where to set this in wrapper.conf?
I have tried like this but the jar is not starting with the parameters from the application.properties.
wrapper.java.command=java
wrapper.java.command.loglevel=INFO
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperJarApp
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=/opt/MyService/lib/MyApp.jar
wrapper.java.library.path.1=../lib
wrapper.logfile=../logs/wrapper.log
wrapper.app.parameter.1=/opt/MyService/lib/MyApp.jar
wrapper.app.parameter.2=-c
wrapper.app.parameter.3=/opt/MyService/lib/conf
Try with this:
wrapper.java.additional.1=-Dspring.config.location=/opt/MyService/lib/conf/application.properties
I see the following warning while starting the jetty9 server as service. I have no idea about this.
WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended. See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
Jetty recommends to run instances of Jetty not from jetty.home distribution folder directly but from jetty.base folder which should be defined separatedly
1. See the chapter Declaring Jetty Base here:
http://www.eclipse.org/jetty/documentation/current/startup-base-and-home.html
The Jetty Distribution's start.jar is the component that manages the
behavior of this separation.
The Jetty start.jar and XML files always assume that both
${jetty.home} and ${jetty.base} are defined when starting Jetty.
You can opt to manually define the ${jetty.home} and ${jetty.base}
directories, such as this:
[jetty-distribution-9.3.7.v20160115]$ pwd
/home/user/jetty-distribution-9.3.7.v20160115
[jetty-distribution-9.3.7.v20160115]$ java -jar start.jar \
jetty.home=/home/user/jetty-distribution-9.3.7.v20160115 \
jetty.base=/home/user/my-base 2013-10-16 09:08:47.802:INFO:oejs.Server:main: jetty-9.3.7.v20160115 2013-10-16
09:08:47.817:INFO:oejdp.ScanningAppProvider:main: Deployment monitor
[file:/home/user/my-base/webapps/] at interval 1 ...
Or you can declare one directory and let the other one be discovered.
The following example uses default discovery of ${jetty.home} by using
the parent directory of wherever start.jar itself is, and a manual
declaration of ${jetty.base}.
[jetty-distribution-9.3.7.v20160115]$ pwd
/home/user/jetty-distribution-9.3.7.v20160115
[jetty-distribution-9.3.7.v20160115]$ java -jar start.jar
jetty.base=/home/user/my-base 2013-10-16
09:08:47.802:INFO:oejs.Server:main: jetty-9.3.7.v20160115 2013-10-16
09:08:47.817:INFO:oejdp.ScanningAppProvider:main: Deployment monitor
[file:/home/user/my-base/webapps/] at interval 1 ...
But Jetty recommends that you always start Jetty by sitting in the
directory that is your ${jetty.base} and starting Jetty by referencing
the start.jar remotely.
2. ... and Creating a new Jetty Base here:
http://www.eclipse.org/jetty/documentation/current/quickstart-running-jetty.html
The demo-base directory described above is an example of the
jetty.base mechanism added in Jetty 9.1. A jetty base allows the
configuration and web applications of a server instance to be stored
separately from the jetty distribution, so that upgrades can be done
with minimal disruption. Jetty's default configuration is based on two
properties: jetty.home
The property that defines the location of the jetty distribution, its libs, default modules and default XML files (typically start.jar,
lib, etc) jetty.base
The property that defines the location of a specific instance of a jetty server, its configuration, logs and web applications (typically
start.ini, start.d, logs and webapps) The jetty.home and jetty.base
properties may be explicitly set on the command line, or they can be
inferred from the environment if used with commands like:
cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar
The following commands: create a new base directory; enables a HTTP
connector and the web application deployer; copies a demo webapp to be
deployed:
JETTY_BASE=/tmp/mybase
mkdir $JETTY_BASE
cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar
WARNING: Nothing to start, exiting ...
Usage: java -jar start.jar [options] [properties] [configs]
java -jar start.jar --help # for more information
> java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy
INFO: server initialised (transitively) in ${jetty.base}/start.d/server.ini
INFO: http initialised in ${jetty.base}/start.d/http.ini
INFO: security initialised (transitively) in ${jetty.base}/start.d/security.ini
INFO: servlet initialised (transitively) in ${jetty.base}/start.d/servlet.ini
INFO: webapp initialised (transitively) in ${jetty.base}/start.d/webapp.ini
INFO: deploy initialised in ${jetty.base}/start.d/deploy.ini
MKDIR: ${jetty.base}/webapps
INFO: Base directory was modified
> cp $JETTY_HOME/demo-base/webapps/async-rest.war webapps/ROOT.war
> java -jar $JETTY_HOME/start.jar
2015-06-04 11:10:16.286:INFO::main: Logging initialized #274ms
2015-06-04 11:10:16.440:INFO:oejs.Server:main: jetty-9.3.0.v20150601
2015-06-04 11:10:16.460:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///tmp/mybase/webapps/] at interval 1
2015-06-04 11:10:16.581:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION!
2015-06-04 11:10:16.589:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2015-06-04 11:10:16.628:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext#1a407d53{/,[file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/, jar:file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.0.v20150601.jar!/META-INF/resources],AVAILABLE}{/ROOT.war}
2015-06-04 11:10:16.645:INFO:oejs.ServerConnector:main: Started ServerConnector#3abbfa04{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2015-06-04 11:10:16.646:INFO:oejs.Server:main: Started #634ms