In Tomcat I want to use a jar inside a web application. The jar file will exist outside of the Tomcat directory.
To include the jar file in tomcat classpath, I modified the TomcatHome/conf/catalina.properties to include the absolute path of my jar file like,
shared.loader=D:\jaa\MyJarFile.jar
as per the suggestion given in link,
http://www.mulesoft.com/tomcat-classpath
But it throws the error,
java.lang.NoClassDefFoundError
I have also tried ,
shared.loader=D:\jaa\*.jar
shared.loader=file:\\D:\jaa\MyJarFile.jar
None of them seem to work :(
If I try placing the jar inside tomcat/lib it seem to work. But I am not allowed to do that.
Please help me out with this issue as I have implementation the next week..
I figured myself how to add the classpath for tomcat. Instead of editing catalina.properties, just create a "setenv.sh" in the Tomcat Bin directory with the classpath,
Example,
CLASSPATH=D:\jaa\MyJarFile.jar
I just checked the catalina.sh in Tomcat/bin and these classpath variable will be set while setting the bootstrap as the classpath.
I was using IntelliJ and I tried everything like:
Using CtrlAltShiftS (Project Settings) and adding a dependency of mysql-connector.jar. Didn't work. (The only thing that worked was that code completion inside IntelliJ was working fine.
Adding mysql-connector.jar to apache-home/lib/ folder. Didn't work.
Including mysql-connector.jar from Maven inside IntelliJ. Just didn't work.
The thing that worked for me:
Include the mysql-connector.jar file in the PROJECT/web/WEB-INF/lib folder.
No need to add it as a dependency anywhere. Just compile this and it will work fine.
I find myself copying extra JARs, which should be available for all contexts and should therefor go into the root loader, into the following directory:
C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib
But this only possible if you have access to this directory, which might not be the case for all ISPs. At least you can do it at home.
Bye
Related
I have log4j2 jars under $CATALINA_HOME/lib:
log4j-api-2.10.0.jar
log4j-core-2.10.0.jar
log4j-jul-2.10.0.jar
export JAVA_OPTS="${JAVA_OPTS} -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
In catalina.properties I've got common classloader and I tried to add log4j-jul-2.10.0.jar again even if it is already under the CATALINA_HOME/lib, but no success.
common.loader="${catalina.base}/lib","${catalina.base}/lib/.jar","${catalina.home}/lib","${catalina.home}/lib/.jar","/opt/tomcat/apache-tomcat-8.5.15/lib/log4j-jul-2.10.0.jar"
I have deleted logging.properties under Tomcat and add a new log4j2.xml to path
ERRORMESSAGE:
Could not load Logmanager "org.apache.logging.log4j.jul.LogManager"
java.lang.ClassNotFoundException: org.apache.logging.log4j.jul.LogManager
Any idea why LogManager is still missing or should I use some other jars instead. In another messages they are speaking juli.jar and extras, but in their case they have older Tomcat version, 6 or 7.
You just need to add the log4j2-api, log4j2-core and log4j2-appserver libraries into the Tomcat classpath, provide the log4j2 configuration file and remove the $CATALINA_BASE/conf/logging.properties from your installation.
This is most easily done by:
Creating a set of directories in catalina home named log4j2/lib and
log4j2/conf.
Placing log4j2-api-2.x.x.jar, log4j2-core-2.x.x.jar, and
log4j2-appserver-2.x.x.jar in the log4j2/lib directory.
Creating a file named log4j2-tomcat.xml, log4j2-tomcat.json,
log4j2-tomcat.yaml, log4j2-tomcat.yml, or log4j2-tomcat.properties
in the log4j2/conf directory.
Create or modify setenv.sh in the tomcat bin directory to include
CLASSPATH=$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf
You can force the applications that use the JUL framework to use log4j2 format changing the environment variable LOGGING_MANAGER. You can do this by adding in the setenv.sh file: LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
Remember that org.apache.logging.log4j.jul.LogManager is included in the log4j-jul-2.x.x.jar bridge which must be added to your classpath.
refs:
https://db-blog.web.cern.ch/blog/luis-rodriguez-fernandez/2019-03-keeping-your-logs-clean-apache-tomcat-9-log4j2-and-spring-boot
https://logging.apache.org/log4j/2.x/log4j-appserver/index.html
I know that this is a little late to answer this question, but I'm sure it could help someone struggling like me trying to configure tomcat so that it uses lo4j.
I've been working on something similar for the past 3 days, and I found out that the extras folder provided by tomcat's website are not what I need. But, you can still grab them using maven. I was able to configure tomcat so that it uses the mentioned jar files ( tomcat-extras-juli.jar and tomcat-extras-juli-adapters.jar ). Just remember to include the VM argument -Dlog4j.debug to make your life easier and catch errors quicker.
Maven repo: https://mvnrepository.com/artifact/org.apache.tomcat.extras/tomcat-extras-juli-adapters
I came upon the same problem after I included the mentioned jars provided by tomcat's repository. After a quick analysis I found that the interface org.apache.juli.WebAppProperties was not included in the jar file tomcat-extras-juli.jar which is utilized by the file org.apache.catalina.loader.WebappClassLoaderBase. After researching a bit more, I realized that the tomcat jar files are included in the Maven Repo. I downloaded the mentioned jar files under the same version of tomcat ( currently 8.5 ), plugged those jars in my tomcat installation and everything worked as expected. Now my version of tomcat uses log4j instead of juli.
log4j2 jars must be loaded along with bootstrap.jar (tomcat startup) and tomcat-juli.jar (logging)
These jars are present in CATALINA_HOME/bin directory and are responsible for
initialization of tomcat including logging.
In CATALINA_HOME/cataline.bat in case of windows, you will find below code -
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
Here, you should add log4j2 jars at the classpath so that when tomcat starts, these jars are there.
Create in tomcat\bin\ file setenv.bat and add to file:
set "CLASSPATH=%CLASSPATH%%CATALINA_BASE%\bin;%CATALINA_BASE%\bin\log4j-core-2.10.0.jar;%CATALINA_BASE%\bin\log4j-api-2.10.0.jar;%CATALINA_BASE%\bin\log4j-jul-2.10.0.jar"
copy jars files
log4j-api-2.10.0.jar
log4j-core-2.10.0.jar
log4j-jul-2.10.0.jar
to folder tomcat\bin\
create file log4j2.xml in tomcat\bin folder
Recently While I was doing a Dynamic web project,I forgot to include OJDBC.jar in the WEB-INF folder.But the code ran file without showing any Class Not Found Exception.But when my friend did the same,Class not found exception was shown.Then when he included the OJDBC.jar in the WEB-INF folder,the code ran fine.I am not able to understand why this is the case.Is it not mandatory to include OJDBC file in WEB-INF folder?And we both used Apache Tomcat Server.
As you can see from the above screenshot,I experimented by not including OJDBC.jar in the WEB-INF folder.The code ran fine.Please tell me why this is the case?
Well, Eclipse builds the context's classpath joining libraries from several locations:
Of corse, the <tomcat_home>\lib\*.jar files.
The <project>\WEB-INF\lib\*.jar files.
... and also the deployment assembly entries (within the project's properties).
Check out every one of these and compare with your partner ones.
this will sound silly but i am executing my code from command prompt and have to use a jar in my class.
I have placed my jar in lib folder of JDK..
but i am still getting error of file not found
any explanation??
EDITED : guys tried all but still not working
EDIT 2 :i am trying to work as was told by this link i am using js-1.6R5.jar
Edit 3 : i undestand all the things you ppl have told but nothing working for me.. pls give me a link to upload my example that i can share with you all.
Edit 4 : i am fed up by setting classpaths but its not working... i have SDK installed in my system, do i need an extra JDK to run my programs from command prompt??
You need to add the jar to the class path by doing the following...
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Please see Wikipedia - Classpath_(Java)
You can place it anywhere, as long is you include it in your classpath. See Setting the Class Path for how to include jars in the classpath.
Have in mind that adding something in the JDK lib is almost never a good idea.
You can make a lib folder in your application's directory and put jar files there, then make your application find them by adding lib to your application's classpath.
And, don't put your jar files in JDK's lib folder. It's not good practise.
You need to let Java know that you want to include the jar in your classpath (the list of folders and jars it checks for classes). One way to do this is with the -cp command line argument, something like
java -cp ".;pathToMyJar\myJar.jar" MyClass
Another is to edit the CLASSPATH environment variable in your OS to include your jar.
A simple solution will be to place the jar fiel inside the windows folder if you are doing it in a Windows machine.
Unfortunately your question contains a lot of question signs and few information.
If you are using java.io.File to open jar as a regular file this jar should not be in lib directory. You just have to provide correct path in file system.
If however you are just trying to use jar as a part of your application it should be in classpath. Use either command line java -cp myjar.jar MyMainClassor put full path to this jar to global environment variableCLASSPATH`.
I'm trying add a directory of jar files (or barring that, each jar file individually) to the classpath for a tomcat instance. The difficulty is that I can't actually modify the /conf/catalina.properties file for this particular problem. I can set the CATALINA_OPTS variable, ie:
export CATALINA_OPTS = "$CATALINA_OPTS
-classpath /path/to/lib/file.jar"
However, this does not seem to add the jar file to the classpath.
Is it even possible to modify the Tomcat classpath like this? I'm aware that the CLASSPATH variable isn't used at all.
You can place your jar files in
{tomcat.home}/lib
They will be loaded from there.
Take a look on script that runs your tomcat. Print classpath just before it runs java process. As far as I remember it is using variable CLASSPATH when composing the command line.
Tomcat has an extension folder, designed to contain these kinds of jars. If I recall correctly it is just a matter of copying your jars to this folder.
I'm trying to recompile a project I've been working on and I keep getting an error message when trying to load a property file:
The system cannot find the path specified.
I guess this has to do with the classpath. But I've added the path to the file in Properties-> Java build path-> Libraries (external class).
I also checked the .classpath file generated by eclipse, and the path is really there!
Why isn't Eclipse looking at the right path?
There 2 different classpaths, build classpath and runtime classpath. The one you are setting is the build classpath.
Check your runtime classpath by going to Run -> Run Configurations and select your application configuration. Check the classpath setting there.
There is another workaround for this also. Eclipse by default will include your output folder (usually named bin) in your classpath. Typically anything that are not compilable in src folder will be copied to bin as is. I assumed your property file is not located in src folder. What you can do is to open your project property and add the folder where your property is located into Java Buld Path -> Source (tab). This way eclipse will copy the content of that folder into bin and will be in the classpath.
There are several ways to read a property file:
Have it in the current working directory (the one cd'ed to). You can do this in the Eclipse launch configuration. (Run -> Run...)
Include it in your application, by having it in a source folder. You then need to read it in through a class loader to be able to get it always (when jarred up, through Java Web Start, etc).
Double check if the property file or its directory is in the excluded list of the project Source. If it is remove the exclusion filter and try recompiling.