Monthly Archives: March 2015

Java program to convert a image into PowerPoint PPT

Here is the Java program that converts (uploads) images into PowerPoint .PPT file using Apache POI libraries.

Use (download) the following combination of Apache POI libraries,

poi-3.11-20141221.jar and poi-scratchpad-3.8.jar

Let’s jump into the program


package com.athavan.examples;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.model.Fill;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

public class CreatePresentation {

private SlideShow ppt;
private Slide slide;
private Fill fill;
private int idx;
private FileOutputStream out;

public static void main(String[] args) {
CreatePresentation cp = new CreatePresentation();
try {
cp.ppt = new SlideShow();
cp.ppt.setPageSize(new Dimension(100, 50));
cp.out = new FileOutputStream(
"G:\\Saravanan\\Pulse\\slideshow2.ppt");

// use the for loop if you have many images to be uploaded into PPT

// for (int k = 0; k < 1; k++) {
String tmp = "G:\\Saravanan\\Pulse\\Software.PNG";
System.out.println(tmp);
cp.createSlideWithChart(cp, tmp);
// }
// Save PPT
cp.ppt.write(cp.out);
cp.out.close();
} catch (Exception fnfe) {
fnfe.printStackTrace();
}
}

public void createSlideWithChart(CreatePresentation cp, String chartName) {
try {
slide = cp.ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
fill.setFillType(Fill.FILL_PICTURE);
idx = cp.ppt.addPicture(new File(chartName), Picture.PNG);
fill.setPictureData(idx);
} catch (FileNotFoundException fnfe) {
} catch (IOException ioe) {
}
}

}

Input File: Software.PNG
Output File: slideshow2.ppt

Output:

The above program converts a image into PPT. If we want to upload more images in a folder into PPT, then we can have for loop (which is commented) to achieve the same.

Happy learning and sharing… 🙂

Advertisements

Exception in thread “main” java.lang.NoSuchFieldError: filesystem at org.apache.poi.hslf.HSLFSlideShow.getPOIFSFileSystem(HSLFSlideShow.java:79)

I was trying to convert a image into PPT using JAVA with the help of Apache POI libraries.

I got the below exception

Exception in thread "main" java.lang.NoSuchFieldError: filesystem
at org.apache.poi.hslf.HSLFSlideShow.getPOIFSFileSystem(HSLFSlideShow.java:79)
at org.apache.poi.hslf.EncryptedSlideShow.checkIfEncrypted(EncryptedSlideShow.java:57)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:141)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:115)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:103)
at org.apache.poi.hslf.HSLFSlideShow.create(HSLFSlideShow.java:167)
at org.apache.poi.hslf.usermodel.SlideShow.(SlideShow.java:117)
at com.athavan.examples.CreatePresentation.main(CreatePresentation.java:25)

Solution:

I was using poi-3.11-20141221.jar and poi-scratchpad-3.2-FINAL.jar. That’s went wrong here. Then I replaced the poi scratchpad with latest version, the error gone.

poi-scratchpad-3.8.jar worked well.

poi-3.11-20141221.jar and poi-scratchpad-3.8.jar combinations worked!!

Happy learning and sharing 🙂

java.lang.IllegalStateException: Web app root system property already set to different value: ‘webapp.root’ = [/ApplicationPath1] instead of [/ApplicationPath2] – Choose unique values for the ‘webAppRootKey’ context-param in your web.xml files!

Recently I have deployed two J2EE web application in tomcat web server. Both application uses the log4j. Tomcat server is up and running with no issues.

But at regular interval, one of the J2EE web application was going down. I was digging into the logs and google to find the solution. At regular times, it made us to enter into the tomcat manager page to start the application manually which was down.

Then I have identified the following exception message

java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files\Apache Software Foundation\Tomcat7.0.57\webapps\application1\] instead of [C:\Program Files\Apache Software Foundation\Tomcat7.0.57\webapps\application2\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:156)
at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:46)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Root cause:

Due to the business requirement, we copied the application1’s code base and created the application2’s code base.
I gave unique names wherever necessary in the configuration files. But still I encountered the above error message. After spending long time, I got the below information about the spring framework

“WARNING: Some containers (like Tomcat) do not keep system properties separate per web app. You have to use unique “webAppRootKey” context-params per web app then, to avoid clashes. Other containers like Resin do isolate each web app’s system properties: Here you can use the default key (i.e. no “webAppRootKey” context-param at all) without worrying. ”

It turns out that the webAppRootKey is a Spring context parameter. It exposes the web application’s root directory as a system property. So, it is the value of the webAppRootKey that provides the name for the system property to use. That’s fine if you’ve just got one webapp in Tomcat. Spring will just choose a default value for this (app.root) and off you go. However, as the warning above notes, Tomcat does not keep system properties separate per webApp. This means that when we get to the second application, the same system property (app.root) is being set to a new value (our second application’s root directory, rather than the first!)

The reason this is occurring at all is that Log4JConfigListener is trying to set the webAppRootKey in both cases. This allows log4J to place log and config files in directories relative to the application root directory, rather than in a fixed path.

I decided the easiest thing was just to use a non-default name for the webAppRootKey for each of my applications.

That is, I fixed this by placing the following context-parameter into each of the web application’s web.xml files:

<code><context-param>
<param-name>webAppRootKey</param-name>
<param-value>uniqueAppRootNamee</param-value>
</context-param></code>

This fixed the problem

Where can we download Spring Framework Jars?

We can download Spring JARs as .zip from the following link

http://maven.springframework.org/release/org/springframework/spring/

Cheers 🙂