javac command line program can use class, need tomcat to do same

This may be simple, but any assistance to a newby would be greatly aprreciated.I have a this class file:

import java.io.*;

public class OCrypt

{

static public String getEncryptedText (String plainText)

{

Runtime R;

String _encryptedText = "";

char[] someData = new char[16];

R = java.lang.Runtime.getRuntime();

try

{

java.lang.Process P = R.exec("./OCrypt " + plainText

);

InputStreamReader ISR = new InputStreamReader(P.getInputStream());

ISR.read(someData);

_encryptedText = new String(someData,0,11);

}

catch (Exception e)

{

_encryptedText = "***ERROR***";

}

return (_encryptedText);

}

}

Where ./OCrypt is a compiled program writtine in C in the same folder.A small test program calls the class and C code and works. Now for the tomcat part, I need to be able to call this code from tomcat. I know it has worked in the past on a much older version of tomcat. I need to figure out how to make it work on tomcat 5.5.9

Thanks

[1091 byte] By [charleston_sca] at [2007-10-3 9:49:10]
# 1

Couple of things

1 - Your class should be in a package. Otherwise you will have problems.

2 -

> Where ./OCrypt is a compiled program writtine in C in the same folder.

In the same folder as what?

The current working directory for Tomcat is normally [TOMCAT]/bin. Is that the folder you mean?

If you want to use this tool, I would say put it in the PATH for the machine (so it can be found from anywhere) or use an absolute reference to it. You can't count on the working directory being where you think it is.

One solution is to use ServletContext.getRealPath().

Is there any reason you couldn't rewrite the encryption into Java - that way you wouldn't have to bother with Runtime.exec()

Another alternative would be to use JNI to get a Java interface to the C class.

http://www-128.ibm.com/developerworks/edu/j-dw-javajni-i.html

I think either of those solutions are preferable to using Runtime.exec().

evnafetsa at 2007-7-15 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the reply. Please bare with me. I am reading in the SAMS teach yourself book to know how to create the folder structure. I assumed this folder should be off of my WEB-INF/classes folder. See the full path below.

> Couple of things

>

> 1 - Your class should be in a package. Otherwise you

> will have problems.

I added the .class file to a package. Or at least I think I did. I added this line to the top of the code.

package OCrypt;

The file is in this folder:

/var/local/jakarta-tomcat-5.5.9/webapps/ROOT/pss/WEB-INF/classes/OCrypt

Then I ran 'javac OCrypt' in that folder.

> 2 -

> > Where ./OCrypt is a compiled program writtine in C

I always keep the C file in the same location as the class. That may be a problem later when tomcat can resolve the class but then can't find that program. I hope that will generate a completely different error.

> in the same folder.

> In the same folder as what?

> The current working directory for Tomcat is normally

> [TOMCAT]/bin. Is that the folder you mean?

I will put it there if I get the class resolved.

> If you want to use this tool, I would say put it in

> the PATH for the machine (so it can be found from

> anywhere) or use an absolute reference to it.

I can put a complete path in the .java file before I compile if you think that will help.

You> can't count on the working directory being where you

> think it is.

But right now I would just like to create a simple class like adding two numbers together and returning a result. My larger issue is more of a server configuration. I am using a enterprise wide tomcat server and have been told EACH application folder has it's own classpath. I even attempted to write a JSP that would output my enviroment to a simple page. This didn't work because the env was only for the user, not the env AFTER all the startup scripts run for the server.

>

> One solution is to use ServletContext.getRealPath().

>

> Is there any reason you couldn't rewrite the

> encryption into Java - that way you wouldn't have to

> bother with Runtime.exec()

>

> Another alternative would be to use JNI to get a Java

> interface to the C class.

> http://www-128.ibm.com/developerworks/edu/j-dw-javajni

> -i.html

>

> I think either of those solutions are preferable to

> using Runtime.exec().

charleston_sca at 2007-7-15 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Except for the part that evnafets posted about using jni, everything else looks fine. You have the classes configured correctly. What's the next step? You want to have a jsp that executes this class?ram.
Madathil_Prasada at 2007-7-15 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Perhaps I am calling something wrong for this version of tomcat. I am using the same code used (that work and is still working) on an order version of tomcat. Here is the import, and call. Anything wrong?

Again, thanks for the help.

<%@ page import="OCrypt"%>

have also tried

<%@ page import="OCrypt.*"%>

String enpassword = OCrypt.getEncryptedText(request.getParameter("password"));

charleston_sca at 2007-7-15 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...