Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Do I need to exec() this program? How?

Thread view: 
Ramon F Herrera - 10 Dec 2007 18:39 GMT
Please consider the justification for an acknowledged "dirty hack" and
its controversial code below.

My actual question will be in my next message.

-Ramon

---------
Working with bootstraps.

The OpenOffice.org Java API has its own methods for bootstrapping
OpenOffice.org. By "bootstrapping" OpenOffice.org, we simply mean
"starting up" or "launching" the OpenOffice.org application launcher.
This is done by finding the location of the juh.jar library and
looking for the soffice(.exe) executable in that location, or in one
directory above that location. This requires the juh.jar library to be
put on the CLASSPATH, enabling the application that you create in this
tutorial to find it. However, here we want to ship our own juh.jar
file (and others) with the application. In this case, this approach to
the bootstrapping mechanism doesn't work.

In order to solve this issue, there are two possibilities. First, it
is possible to make sure that Java can find the soffice(.exe)
executable every time. This can be done by putting the directory
containing the executable on the PATH in Windows, or on
LD_LIBRARY_PATH in Mac, Unix, and Linux. This requires action from
potential users and we want to prevent that.

So we choose the second possibility, which involves working with
access modifiers. In the Sun JDK, the system ClassLoader is an
instance of the class URLClassLoader. This class has a private method
called addURL, which is called when Java starts and which will add all
JARs and other resources needed. Using Reflection, we request an
instance of the URLClassLoader, make the addURL method accessible to
us, and add the directory containing the soffice(.exe) executable to
the stack of URLs in the URLClassLoader. It is a dirty hack, but it
works.

-----------------

and this is the questioned code:

public Object simpleBootstrap
       (String pathToExecutable) throws Exception {

   //Get the executable from the incoming String:
   String ooBaseDirectory = pathToExecutable.replaceAll("soffice(.exe)
{0,1}$","");
   System.out.println("Your ooBaseDir is: " + ooBaseDirectory);

   ClassLoader loader = ClassLoader.getSystemClassLoader();
   if (loader instanceof URLClassLoader) {
       URLClassLoader cl = (URLClassLoader)loader;
       Class sysclass = URLClassLoader.class;
       try {
           Method method = sysclass.getDeclaredMethod("addURL", new
Class[]{URL.class});
           method.setAccessible(true);
           method.invoke(cl, new Object[]{new
File(ooBaseDirectory).toURL()});
       } catch (Throwable t) {
           t.printStackTrace();
           throw new IOException("Error, could not add URL to system
classloader");
       }
   } else {
       System.out.println("Error occured, URLClassLoader expected but
" +
           loader.getClass() + " received. Could not continue.");
   }

(If you are inside an IDE, you don't need any of the above "dirty
hack", you only need the line below):

   //Get the office component context:
   XComponentContext xContext = Bootstrap.bootstrap();
Ramon F Herrera - 10 Dec 2007 18:48 GMT
> Please consider the justification for an acknowledged "dirty hack" and
> its controversial code below.
[quoted text clipped - 72 lines]
>     //Get the office component context:
>     XComponentContext xContext = Bootstrap.bootstrap();

For more context see this:

http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/8436e
4f4f83fffbb
#
http://tinyurl.com/2ccpm4

-Ramon
Ramon F Herrera - 10 Dec 2007 19:26 GMT
> Please consider the justification for an acknowledged "dirty hack" and
> its controversial code below.
[quoted text clipped - 72 lines]
>     //Get the office component context:
>     XComponentContext xContext = Bootstrap.bootstrap();

Here's the question. The author of the tutorial writes this:

"In order to solve this issue, there are two possibilities. First, it
is possible to make sure that Java can find the soffice(.exe)
executable every time. This can be done by putting the directory
containing the executable on the PATH in Windows, or on
LD_LIBRARY_PATH in Mac, Unix, and Linux. This requires action from
potential users and we want to prevent that."

Well, it turns out that between the "dirty hack"(*) and the above
solution, I definitely prefer the latter. Let's assume that my program
will only run on Windows, for now.

Doubts:

- I don't think that simply placing the OpenOffice directory in the
Windows PATH is enough, right? I suppose that I still need to 'exec()'
the OpenOffice executable?

- Is the author talking about PATH or CLASSPATH (it seems like
CLASSPATH, based on the LD_LIBRARY being mentioned)?

- Let's say that I perform the exec:

 commandLine = "C:/Program Files/OpenOffice.org 2.3/program/
soffice.exe";
 p = Runtime.getRuntime().exec(commandLine);

I still need to reconcile the above line with the rest of the program:

 XComponentContext xContext = Bootstrap.bootstrap();

I bet the following shot in the dark will not work, will it?:

 XComponentContext xContext = (XComponentContext) p;
 [rest of the program follows normally...]

I am really stuck with this, and have been for a while. Any help is
much appreciated.

-Ramon

(*) Justly qualified by Owen as:

 "I shudder to read such code.  Eegh."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.