Getting tomcat's installation directory using java - java

I want to get tomcat's installation directory in my computer using java. I tried using :
System.getProperty("catalina.base");
and
System.getProperty("catalina.home");
But both methods return null as the answer. I tried it with System.getProperty("java.home"); and it correctly returns the java path.
Any ideas as to what the problem is? Thanks

Try installing this JSP and passing various values for the "property" parameter:
<%
String propertyName = request.getParameter("property");
Object propertyValue;
String typeString;
if(null == propertyName)
propertyValue = null;
else
propertyValue = System.getProperty(propertyName);
if(null == propertyValue)
typeString = "null";
else
typeString = propertyValue.getClass().getName();
%>
The system property <code><%= propertyName %></code> has the value:
<code><%= propertyValue %></code> (<%= typeString %>).
Maybe you can find a pattern to which property values return null.

I also encountered the same error as you did. but then I realized that the tomcat server was not running. After starting the server, I still failed to get the tomcat path.
Then I realized that I was trying to get the path from the main method in a JSF project while tomcat was still running.
So i finally got the tomcat path by using: System.out.println(System.getProperty("catalina.base")); in a #PostConstruct method of one of my #ViewScoped beans in a JSF project. And it successfully displayed C:\Documents and Settings\S!LENT W#RRIOR\Application Data\NetBeans\7.2.1\apache-tomcat-7.0.27.0_base in the console in NetBeans.
So In order to get Tomcat's installation directory, following points should be kept in mind:
Tomcat Server is running
You're not trying to get path from main method
hope this helps

we are using cucumber for testing and in cucumber cases, we are accessing project's individual method. in project, we are reading configuration for xml and the location is in config folder of Apache tomcat. Now when i run project,System.out.println(System.getProperty("catalina.base")); it's provide the proper path however when i call project's method from cucumber then it's returning null

Related

Jetty resource base different in Maven Nested Multi-module project of IntelliJ IDEA vs Eclipse

We have a nested multi-module project. Our developers are a mix of IntelliJ IDEA and Eclipse users.
When running a jetty server inside an inner module, it seems we need to set the resource base to different values depending on which IDE we are using.
For IntelliJ:
root.setResourceBase("myModule/src/main/webapp");
For Eclipse:
root.setResourceBase("src/main/webapp");
We don't want to have to tweak our IDEs to make it work, e.g. I don't want to have to change some setting in IntelliJ to make it work with the Eclipse version of the code.
Any ideas?
The short answer:
Your execution differences between Eclipse vs Intellij can be explained by having different PWD, or ${user.dir}, or working directory setups.
The better answer:
Don't use filesystem paths then.
Look up a known resource in that location via a Classloader.getResource() and then pass the parent directory into the root.setResourceBase()
Example:
Server server = new Server(8080);
// Figure out what path to serve content from
ClassLoader cl = WebAppContextFromClasspath.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("hello.html");
if (f == null)
{
throw new RuntimeException("Unable to find resource directory");
}
// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(webRootUri.toASCIIString());
webapp.setParentLoaderPriority(true);
server.setHandler(webapp);
server.start();
server.join();
You can see this in the embedded-jetty-cookbook examples:
WebAppContextFromClasspath.java
ResourceHandlerFromClasspath.java
The other better answer:
Another approach is to find the src/main/webapp a few different ways depending on how it is being run
See the operational modes in the ServerMain.java in the embedded-jetty-live-war example.

access via spring injected property which references a directory to access its files

I'm trying to access a property defined in a bean like this:
<bean id="reportdepositService" class="a.b.c.ServiceImpl">
<property name="reportDeposit" value="/WebContent/WEB-INF/dirName/" />
</bean>
ServiceImpl class looks like this:
public class ServiceImpl implements IService {
private Resource springResource;
public Resource getSpringResource() {
return springResource;
}
public void setSpringResource(Resource springResource) {
this.springResource = springResource;
}
private File getSpringResourceFile() throws IOException{
Resource r = getSpringResource();
URL url = FileLocator.resolve(r.getURL());
return FileUtils.toFile(url);
}
public void doSomething(){
.. some logic .
File f = getSpringResourceFile();
}
executing that code within eclipse on a ubuntu machine works fine, application build on a jenkins works fine as well on ubuntu. Running that application on a win7/64, the code throws the following exception:
OSGi resource[/WebContent/WEB-INF/springResource/|bnd.id=332|bnd.sym=a.b.server] cannot be resolved to URL because it does not exist
java.io.FileNotFoundException: OSGi resource[/WebContent/WEB-INF/springResource/|bnd.id=332|bnd.sym=a.b.server] cannot be resolved to URL because it does not exist
at org.springframework.osgi.io.OsgiBundleResource.getURL(OsgiBundleResource.java:228)
What is necessary to access the property on windows hosted system?
Any ideas?
Thanks in advance
I am not sure whether you are on the correct road with this:
The /WebContent/WEB-INF path suggests that you are writing a web application to be run in a web container. In that case you should never assume that your resource is a file. You should open the resource using Resource.getInputStream() in stead of using the URL/File. The reason is that the the application may well be run directly from the .war without a file system being available.
That may immediately answer the question: is the Windows 7 environment the same in relation to the run-enviroment? My impression is that it is not. If you bundled the project into a bundle jar while transporting it to the windows machine, I guess you should add a prefix to the path (but probably need to leave the WebContent off), like bundle:, classpath:, etc. See Spring OSGI reference. But you need to give a little more information to be sure.

Problems with Velocity Resource Path

So, here's the deal.
I'm using Spring Framework to develop a appointment app.
Everything's going fine in my localhost, even the email send part.
But when i pass the project to my weblogic, the resource.loader.path property appears not to load.
Here's the important part of my code:
Properties prop = new Properties();
prop.setProperty("resource.loader", "class");
prop.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
prop.setProperty("class.resource.loader.path", "../jsp/email-templates");
VelocityEngine.init(prop);
Template template = VelocityEngine.getTemplate("user-response.jsp");
As i said, just the important part of my code. Basically is configured like this with a Properties Object and the VelocityEngine.getTemplate() loading the user-response.jsp file that are inside the folder mentioned above.
As i said, in my localhost, he just works fine but in weblogic appears that way:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '..nulluser-response.jsp'
For some reason, the class.resource.loader.path property do not load in weblogic.
So... someone has already been through this problem? Any ideias of what's wrong?
Obs.: The two projects (localhost and weblogic) are the same, using a version control software (bazaar)
I'd recommend making it relative to the CLASSPATH. If you put the /email-templates folder under WEB-INF/classes and make the load path "email-templates" it should work.

Can't find property files on classpath

I'm trying to load/read property files on glassfish 3.1 server.
I can't get it to work. I've searched and tried many possible solutions.
None worked so far, always NULL as result.
I've tried methods from the following link:
How to use a property with Glassfish
My method:
final public class GTS_Properties{
...
public static Properties getPropertiesFromFile(String fileName){
URL url = GTS_Properties.class.getResource(fileName)
...
// url is always null
}
}
My configuration:
Folder properties on build path.
/root/WEB-INF/classes/****.properties
How the Glassfish server looks like (using eclipse startup plugin)
/glassfish-root/domain/eclipseApps/MyWebApp/WEB-INF/classes/****.properties
I have tried to put the property files directly in WEB-INF instead of classes. same result.
I solved it by first going 2 'parent folders up' from the startpoint of the relative path.
Inserting "../../" on the start of the path was all that was needed.
WEB-INF/classes/WebApp/utils/GTS_Properties.class (The class to get the resource from)
WEB-INF/classes/prop.properties (Location of the resource file)
GTS_Properties.class.getResource("../../prop.properties")

How to set Spring Classpath

I am using IntelliJ and jus checked out working code from the svn.I am struggling to run the jar.
Its a simple core java Spring project.
Since I get the above error.I understand that the spring path is not set fine.
How do I handle it.?
private ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationRepositoryConfiguration.class);
Application context file is a bean class here (#Bean annotation is used). I am not using a xml file.
It seems like you are running your project from command line. Run following command :
java -classpath spring.jar;spring-sec.jar,......so on com.example.UrMainClass
while specifying jar in command, make sure you provide complete path of jar. Also check this link https://stackoverflow.com/a/10122038/1065180

Categories