0

I have created a executable jar of a Java program and I want to access it via my Servlet on the same machine or it can be on the other. How can I do so?

I found many solutions of calling Servlet by Java program using networking/TCP... but I am searching for Servlet to call function of Java program.

4 Answers 4

1

Calling a function of JavaProg means spawning another JVM process which is very expensive. Why don't you just put this jar (you wrote you're creating this program, so you know how its built) into your web application and make your serlvet just call appropriate classes?

Is is a viable option at all?

Sign up to request clarification or add additional context in comments.

2 Comments

Barmnik Ok ,Its it like you means to import the classes into my servlet and call the function ... Its acceptable , but then really will there be any sence for creating jar of it ? PLs explain
Sure, its a common way actually to develop web applications, you can even put put your classes in the same module with the servlet. Just keep in mind that the servlet once called is a regular java object that can create and call other java objects. The servlet is usually just an entry point of your application on the server side, its usually quite a "thin" layer, the main processing (algorithms/db) is usually done in Java classes accessible from the servlet
1

There is no much difference between java programs and Java servlets. So you just import the java programs and it can be directly used inside you java servlet.

However, one thing to note is that the java program you want to import, that is your jar file have to be put inside WEB-INF/class/ directory.

Comments

1

Found another solution for this which is more specific

If you can run everything in the same JVM, you could do something like this:

public class Launcher { ... public static void main(String[] args) throws Exception { launch(Class.forName(args[0]), programArgs(args, 1)); }

protected static void launch(Class program, String[] args) throws Exception {
    Method main = program.getMethod("main", new Class[]{String[].class});
    main.invoke(null, new Object[]{args});
}

protected static String[] programArgs(String[] sourceArgs, int n) {
    String[] destArgs = new String[sourceArgs.length - n];
    System.arraycopy(sourceArgs, n, destArgs, 0, destArgs.length);
    return destArgs;
}

And call the method launch with supplied/configured parameters.

Comments

0

You can simpley add your jar into your servlet application.Imort that jar in your servlet and call appropriate class which you want to use in your servlet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.