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

R

Ramon F Herrera

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();
 
R

Ramon F Herrera

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();


For more context see this:

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

-Ramon
 
R

Ramon F Herrera

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();


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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top