dev@glassfish.java.net

Re: 【Confirmation】About "Getting started with HK2 - Part I"

From: Tang Yong <tangyong_at_cn.fujitsu.com>
Date: Tue, 28 Feb 2012 11:58:28 +0900

Hi Andriy,

I have known how to resolve the WARNING No Main-Bundle module...

Firstly,

> Error: Unable to access jarfile lib/hk2.jar
My thinking is that because using "java -jar hk2-$HK2_VERSION.jar" is
ok, needing not to rename/copy hk2-$HK2_VERSION.jar to hk2.jar.

Secondly,

About Warning, I debugged and investigated the hk2 souce and had the
following conclusion.

(1) add the following entry to manifest.mf of hk2-core.jar

HK2-Main-Bundle: sahoo.hello-startup

Note:
 "sahoo.hello-startup" is hk2's module definition's name(equaling
"Bundle-SymbolicName"), and in the current hk2's source, hk2's module
definition's name has not version info.

(2) On the cmdline, after executing "java -jar hk2-$HK2_VERSION.jar"
again, the following info appeared(No Warning!):

F:\GlassfishV3\samples\hk2\sahoo\hello-world-hk2-sample>java -jar
./lib/hk2-2.0.0-SNAPSHOT.jar
Hello World - My first HK2 Sample

Finally, I have a suggestion:

According to
"hk2-core/src/java/com/sun/enterprise/module/ManifestConstants.java",

The role of "HK2-Main-Bundle" is :

"This is equivalent of "Main-Class" for us. We'll find {_at_link
ModuleStartup} from this module."

Because the fact that if you don't add the "HK2-Main-Bundle" to
hk2-core.jar, you can still launch the "hello-world-hk2-sample", so,
why not deleting the "HK2-Main-Bundle"-related codes?

--Best Regard!
--Mike Tang

Andriy Zhdanov wrote:
> Hi Mike Tang,
>
> Sorry, can't understand you.
>
> 1) Remove copying of hk2.jar and clean lib dir, then there is no hk2.jar
> for java -jar hk2.jar:
>
> Error: Unable to access jarfile lib/hk2.jar
>
> 2) Removing class name in java -jar does not change anything.
>
> WARNING: No Main-Bundle module found in manifest of
> /opt/console.next/ctm/hello-world-hk2-sample/lib/hk2-core.jar
> com.sun.enterprise.module.bootstrap.BootException: No module has a
> ModuleStartup implementation
> at
> com.sun.enterprise.module.bootstrap.Main.findStartupService(Main.java:398)
>
> Once again, problem is that HK2 looks up for main module in bootstrap
> jar, which is hk2-core.jar in the case, because it is where
> com.sun.enterprise.module.bootstrap.Main is, as specified in Main-class
> in hk2.jar.
>
> com.sun.enterprise.module.bootstrap.Main (Main.java:153):
> String targetModule = findMainModuleName(bootstrap);
>
> bootstrap=/opt/console.next/ctm/hello-world-hk2-sample/lib/hk2-core.jar
> targetModule=null
>
> Also notice, hk2.jar!/META-INF/MANIFEST.MF has the following class path:
>
> Class-Path: hk2-core.jar class-model.jar config.jar auto-depends.jar j
> avax.inject.jar asm-all-repackaged.jar hk2-api.jar osgi-resource-loca
> tor.jar tiger-types.jar bean-validator.jar jtype.jar
>
> This is why run.sh renames all hk2-*-$HK2_VERSION.jar to hk2-*.jar into
> lib dir.
>
> Well, I've just made another unsuccessful attempt - let HK2 find
> MyStartup module by ModuleStartup contract (see
> com.sun.enterprise.module.bootstrap.Main.findStartupService(Main.java:394)
>
> Adding Main-Class to hello-startup.jar and using it for java -jar
> launcher, helps a bit, but it still does not work:
>
> NullPointerException
> at
> com.sun.enterprise.module.bootstrap.Main.findStartupService(Main.java:435)
>
> Because classloader of MyStartup class is not ModuleClassLoader. No
> ideas furthermore.
>
> See also changes
> <https://github.com/avalez/hello-world-hk2-sample/commit/4d3f411>.
>
> Thank you.
>
> On 02/25/2012 12:37 PM, Tang Yong wrote:
>> Hi Andriy,
>>
>> Thanks your reply quickly!
>>
>> I have seen the run.sh in your github's sample and exeucted your run.sh.
>> The result is ok! But I have few suggestions on your run.sh.
>>
>> (1) The following cp is not needed because hk2-$HK2_VERSION.jar is main
>> module.
>>
>> cp $MAVEN_LOCAL_REP/org/glassfish/hk2/hk2/$HK2_VERSION/
>> hk2-$HK2_VERSION.jar lib/hk2.jar
>>
>> (2) The following sentence
>> "
>> $JAVA_HOME/bin/java -jar lib/hk2.jar
>> com.sun.enterprise.module.bootstrap.Main"
>>
>> should be modified to
>>
>> "
>> $JAVA_HOME/bin/java -jar lib/hk2-$HK2_VERSION.jar"
>>
>> because in hk2-$HK2_VERSION.jar, there is an executable entry.
>> "Main-Class: com.sun.enterprise.module.bootstrap.Main"
>>
>> --Best Regard!
>> --Mike Tang
>>
>> Andriy Zhdanov wrote:
>>> Hi Mike,
>>>
>>> I've been also recently struggling with the sample, and have not managed
>>> to make it work with run.sh, it appears main module is being looked for
>>> inside jar with Bootstrap class (in hk2-core.jar particularly), and
>>> setting mainModule does not work either, but it works for maven:
>>>
>>> mvn -f hello-startup/pom.xml -DmainModule=sahoo.hello-startup hk2:run
>>>
>>> So may be Sahoo will comment on this, but I believe you can work it out
>>> with maven too.
>>>
>>> If you'd also like, see my updated version of it here
>>> <https://github.com/avalez/hello-world-hk2-sample>.
>>>
>>> Thank you.
>>>
>>> On 2/24/12 3:52 PM, Tang Yong wrote:
>>>> Hello Dear Sahoo,HK2 DevTeam,
>>>>
>>>> About HK2's using way, I found that according to Sahoo's weblog("Getting
>>>> started with HK2 - Part I"), when I ran the "hello-world-hk2-sample" in
>>>> windows platform using the following way, I ran the following error.
>>>>
>>>> 【Running Way(according to "run.sh")】
>>>> (1) Copy auto-depends-$HK2_VERSION.jar,config-$HK2_VERSION.jar,
>>>> hk2-core-$HK2_VERSION.jar,hk2-$HK2_VERSION.jar,tiger-types-1.4.jar,
>>>> hello-startup-1.0-SNAPSHOT.jar to the new created "lib"
>>>> (2) java -jar ./lib/hk2-2.0.0-SNAPSHOT.jar
>>>>
>>>> Note:
>>>> Because I built hk2 from trunk, the $HK2_VERSION which I used is
>>>> hk2-2.0.0-SNAPSHOT.
>>>>
>>>> 【Error Info】
>>>> Exception in thread "main" java.lang.NoClassDefFoundError:
>>>> com/sun/enterprise/module/bootstrap/Main
>>>> Caused by: java.lang.ClassNotFoundException:
>>>> com.sun.enterprise.module.bootstrap.Main
>>>> ...
>>>>
>>>> 【Investigation】
>>>> Then, I investigated for a while and have the following result:
>>>> (1) In hk2-2.0.0-SNAPSHOT.jar's Manifest.MF, there are the following
>>>> descriptions(★Dependency JARs have not $HK2_VERSION):
>>>>
>>>> ”
>>>> Class-Path: hk2-core.jar class-model.jar config.jar auto-depends.jar j
>>>> avax.inject.jar asm-all-repackaged.jar hk2-api.jar osgi-resource-loca
>>>> tor.jar javax.inject.jar tiger-types.jar bean-validator.jar jtype.jar
>>>> ”
>>>>
>>>> So, I modified the [Running Way] using the following:
>>>>
>>>> Copy auto-depends.jar,config.jar,hk2-core.jar,tiger-types.jar to the new
>>>> created "lib" again.
>>>>
>>>> To be pity, the same error info still appeared.
>>>>
>>>> (2) Then,apart from the above jar, when I added class-model.jar,
>>>> asm-all-repackaged.jar,hk2-api.jar and javax.inject.jar to the new
>>>> created "lib" and executed the sample again, error disppeard and result
>>>> is OK!
>>>>
>>>> “
>>>> 2012/02/24 23:35:13 com.sun.enterprise.module.bootstrap.Main
>>>> findMainModuleName warning: No Main-Bundle module found in manifest of
>>>> F:\GlassfishV3\samples\hk2\sahoo\hello-world-hk2-sample\lib\hk2-core.jar
>>>> Hello World - My first HK2 Sample
>>>> ”
>>>> So, I wish that if Dear Sahoo has free time, you can confirm whether
>>>> the run.sh in the weblog is wrong or not. In addition, I want to know
>>>> when packaging hk2-$HK2_VERSION.jar, in the "Class-Path:"why not adding
>>>> the $HK2_VERSION to the dependency jars?
>>>>
>>>> --Thanks
>>>> --Best Regard
>>>> --Mike Tang(OSGi Fans)
>>>>
>>>>
>