views:

266

answers:

2

I am trying to load jar file using JCL using following code

FileInputStream fis = new FileInputStream(new File( "C:\\Users\\sunils\\glassfish-tests\\working\\test.jar") );
        JarClassLoader jc = new JarClassLoader(  );
        jc.add(fis);
        Class main = jc.loadClass( "highmark.test.Main" );
        String[] str={};

        main.getMethod("test").invoke(null);//.getDeclaredMethod("main",String[].class).invoke(null,str);
        fis.close();

But when I try to run this program I get Exception as

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at Main.main(Main.java:21)
Caused by: java.lang.RuntimeException: Embedded startup not found, classpath is probably incomplete
    at org.glassfish.api.embedded.Server.<init>(Server.java:292)
    at org.glassfish.api.embedded.Server.<init>(Server.java:75)
    at org.glassfish.api.embedded.Server$Builder.build(Server.java:185)
    at org.glassfish.api.embedded.Server$Builder.build(Server.java:167)
    at highmark.test.Main.test(Main.java:33)
    ... 5 more

According to this it is not able to locate class, But when I run the jar file explicitly it runs fine. It seems like JCL is ignoring other classes present in the jar file,

MANIFEST.MF file in jar file shows:

Manifest-Version: 1.0
Class-Path: .
Main-Class: highmark.test.Main

It seems to be ignoring Class-Path: . , This jar file runs fine when I run it using Java explicitly, This is just a test, in reality this jar file is coming as a InputStream and it cannot be stored in filesystem, How can I overcome this problem , Is there any workaround ?

Thanks for any help .

UNDATE: Here is a jar Main class :

package highmark.test;
import org.glassfish.api.embedded.*;

import java.io.*;

import org.glassfish.api.deployment.*;

    import com.sun.enterprise.universal.io.FileUtils;
    public class Main {
        public static void main(String[] args) throws IOException, LifecycleException, ClassNotFoundException {
            test();     
        }
        public static void test() throws IOException, LifecycleException, ClassNotFoundException{

        Server.Builder builder = new Server.Builder("test");

        Server server = builder.build();
        server.createPort(8080);
        ContainerBuilder containerBuilder = server.createConfig(ContainerBuilder.Type.web); 
        server.addContainer(containerBuilder);
        server.start();


        File war=new File("C:\\Users\\sunils\\maventests\\simple-webapp\\target\\simple-webapp.war");//(File) inputStream.readObject();
        EmbeddedDeployer deployer = server.getDeployer();
        DeployCommandParameters params = new DeployCommandParameters();
        params.contextroot = "simple";
        deployer.deploy(war, params);
        }
    }
A: 

Make sure that the manifest ends with a newline, or the last line may not parsed properly. Also, if your jar file does not need outside resources, you may not need to specify a class-path entry. You may only need to specify a class-path if there are needed resources within external jar files and directories. Here's an example from the tutorials:

We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.

We first create a text file named Manifest.txt with the following contents:

Class-Path: MyUtils.jar
YGL
A: 

Use JarClassLoader from http://www.jdotsoft.com/JarClassLoader.php

It's much simpler to use.

gorokhmn