Forum Stats

  • 3,875,875 Users
  • 2,266,977 Discussions
  • 7,912,363 Comments

Discussions

How to start the display of a ppt-file from Java Desktop application?

843810
843810 Member Posts: 46,938
edited Feb 4, 2010 10:57AM in Java Desktop Applications
Is it possible to start a PowerPointPresentation (ppt / pptx-file) with Java?
Opening the file simply works with Desktop.open.
Can I somehow pass a parameter, so that the ppt-file starts in display mode?
Currently I ask my customers to convert to pps - but they find that difficult.
There seems to be a way with http://j-integra.intrinsyc.com/support/com/doc/#other_examples/Powerpoint_from_Java.htm
Also asked in jacob forum if that could be helpful.
Unfortunately, I'm not yet too familiar with JNI, so I'd need an example
Any help appreciated.
«1

Comments

  • wellington7
    wellington7 Member Posts: 157
    So, what's wrong with this example you mention at j-integra website?
  • darrylburke
    darrylburke Member Posts: 18,007
    You can use Runtime.exec or ProcessBuilder to launch PowerPoint with appropriate parameters for the filename and mode (i.e. slideshow). You can research the correct parameters in Folder Options or search on a Microsoft support site. It'll probably be something like
    POWERPNT.EXE /s "filename"
    db
  • 843810
    843810 Member Posts: 46,938
    If I redistribute the application j-integra wants a licence fee for every computer on which it is installed
  • 843810
    843810 Member Posts: 46,938
    Thanks for your post.
    Do you have a hint, how to find the powerpnt.exe elegantly
    Searching the whole registry like recommended
    here might be errorprone too, don't you think?
  • darrylburke
    darrylburke Member Posts: 18,007
    If MS Office was installed in the regular way and the computer / registry hasn't been tampered with, it'll be on the PATH and will be found.

    db
  • 843810
    843810 Member Posts: 46,938
    when trying
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("powerpnt.exe c:/test.ppt");

    on WinXp with normal Office 2007 I get
    java.io.IOException: Cannot run program "powerpnt.exe": CreateProcess error=2, System can't find file powerpnt.exe
  • wellington7
    wellington7 Member Posts: 157
    Try without the extension .exe
    What if you try to run it without the parameter file, it runs?
    And if try to run winword, does it run?
  • wellington7
    wellington7 Member Posts: 157
    edited Feb 3, 2010 6:00PM
    I made some tests here, and could not run nothing from command either. And I found this at google.
    The working code is at the 4th page.
  • wellington7
    wellington7 Member Posts: 157
    edited Feb 3, 2010 6:23PM
    A little more:

    In my tests to adaptate the code to my interests, the code was throwing a exception.. After a little work i saw the os.name was returning "Windows XP" instead of the expected "Windows NT". So I just changed the line:
    if (osName.equals("Windows NT") || osName.equals("Windows XP")) 
    But the swriter command (to open the Open Office writer) still was not running.
    I realized that, to run some programs from the command you need use start swriter (in your case start powerpnt).

    Tell if it works.
  • 843810
    843810 Member Posts: 46,938
    It works!!
    Thanks a lot for your help.
    You have a paypal account? I'd be happy to give you a reward

    I refactored the class a little and when using
    start powerpnt.exe /s filename
    it works.
    import java.util.*;
    import java.io.*;
    
    
    /*
     * Refactored my mbisping after example from
     * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
     */
    public class GoodWindowsExec {
    	
    	public static void main(String args[]) {
    		if (args.length < 1) {
    			System.out.println("USAGE: java GoodWindowsExec <cmd>");
    			System.exit(1);
    		}
    		try {
    			exec(args[0]);
    		} catch (Throwable t) {
    			t.printStackTrace();
    		}
    	}
    
    	public static void exec(String command) throws IOException,
    			InterruptedException {
    		String osName = System.getProperty("os.name");
    		System.out.println(osName);
    		String[] cmd = new String[3];
    		if (osName.equals("Windows 95") || osName.equals("Windows 98")) {
    			cmd[0] = "command.com";
    		} else //newer Windows versions use cmd.exe
    		{
    			cmd[0] = "cmd.exe";
    		}
    		cmd[1] = "/C";
    		cmd[2] = command;
    
    		Runtime rt = Runtime.getRuntime();
    		System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
    		Process proc = rt.exec(cmd);
    		// any error message?
    		StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),
    				"ERROR");
    
    		// any output?
    		StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),
    				"OUTPUT");
    
    		// kick them off
    		errorGobbler.start();
    		outputGobbler.start();
    
    		// any error???
    		int exitVal = proc.waitFor();
    		System.out.println("ExitValue: " + exitVal);
    	}
    	
    }
    class StreamGobbler extends Thread {
    	InputStream is;
    	String type;
    
    	StreamGobbler(InputStream is, String type) {
    		this.is = is;
    		this.type = type;
    	}
    
    	public void run() {
    		try {
    			InputStreamReader isr = new InputStreamReader(is);
    			BufferedReader br = new BufferedReader(isr);
    			String line = null;
    			while ((line = br.readLine()) != null)
    				System.out.println(type + ">" + line);
    		} catch (IOException ioe) {
    			ioe.printStackTrace();
    		}
    	}
    }
This discussion has been closed.