0

I created a simple jar (helloword.jar) that prints "Hello World", i want to run it from my JSP web page that is running in an apache-tomcat server. The file helloword.jar is in the same porject/folder that contains my JSP page.

This is my code for the JSP and it is not showing the message "Hello World" in the tomcat console nor web-broswer console. (I imported the jar in the libraries in my NetBeans IDE)

<%@ page import="helloword.*" %>
<%
        helloword hw = new helloword();
        hw.main();

               %>

I tried this too but it didnt work neither.

Runtime.getRuntime().exec("java -jar helloword.jar");
Mr Pibe
  • 13
  • 1
  • 6

2 Answers2

0

While creating the jar , write a method other than main for example

public static void display()
{
  System.out.println("Hello World!");
}

step1: put the jar in web-inf lib folder. step2: import jar in page tag. step3: call this method directly by class name.

Ajay Jayavarapu
  • 471
  • 1
  • 6
  • 16
0

If you want to execute jar dynamically, you need to add jar to jvm context, then create and instance of Main class, and then run main method.

URLClassLoader child = new URLClassLoader (myJar.toURL(), this.getClass().getClassLoader());
Class clazz = Class.forName ("com.MyClass", true, child);
Method method = clazz.getMethod("methodName", String.class);
Object o = method.invoke(null, "whatever");
Adem
  • 9,402
  • 9
  • 43
  • 58