5 messages in net.java.dev.jna.usersRe: [jna-users] Thread's ContextClass...
FromSent OnAttachments
Thomas BörkelDec 4, 2008 3:52 am 
Timothy WallDec 10, 2008 11:34 am 
Thomas BörkelDec 12, 2008 4:01 am 
Timothy WallDec 12, 2008 5:19 am 
Thomas BörkelDec 12, 2008 11:52 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Thread's ContextClassLoader is null for Win32 servicesActions...
From:Thomas Börkel (tho@boerkel.de)
Date:Dec 12, 2008 4:01:49 am
List:net.java.dev.jna.users

HI!

I ran into problems, because XMLEncoder and XMLDecoder (serialize Beans to/from XML) throw exceptions, when ContextClassLoader is null.

So, that maybe a bug in those classes.

Thomas

Timothy Wall wrote:

I don't think there's any requirement that the context class loader be non-null. Chances are the threads you're running on are different whether you're running on the console or as a service.

As a service, some threads are likely to be native threads which get attached to the VM on callbacks, which most certainly won't be the same as the console version.

On Dec 4, 2008, at 6:53 AM, Thomas Börkel wrote:

HI!

When my Java program is being started in the console, the ContextClassLoader (Thread.currentThread().getContextClassLoader()) is set.

But when the same program runs as Win32 service, the ContextClassLoader is null!

This also applies to new threads, that are being created by the program, not only the main thread.

Anyone any idea, why?

Attaching slightly modified TestService from JNA/contrib to show the problem.

Start with "java.exe -cp ... jnacontrib.win32.TestService x" and also as service.

The command line is exactly the same (besides the "x").

The ContextClassLoader is being written to C:\TestService.log.

Thanks!

Thomas /* * TestService.java * * Created on 12. September 2007, 12:49 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */

package com.apag.p2plus.p2core.win32;

import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger;

/** * TestService. */ public class TestService extends Win32Service {

/** * main. * @param args arguments */ public static void main(String[] args) throws Exception { TestService service = new TestService();

if(args.length == 1) {

if(args[0].equalsIgnoreCase("install")) { System.out.println(service.install("TestService DisplayName", "TestService Description", null, null, null));

} else if(args[0].equalsIgnoreCase("uninstall")) { System.out.println(service.uninstall());

} else if(args[0].equalsIgnoreCase("query")) { System.out.println(service.getStatus());

} else { System.out.println("Arguments:"); System.out.println("install = install service"); System.out.println("uninstall = uninstall service"); System.out.println("<none> = run service"); testClassLoader(); System.exit(0); }

} else { service.init(); } }

/** * Creates a new instance of TestService. */ public TestService() { super("TestService"); }

/** * Will be called on start. */ public void onStart() { log("onStart"); testClassLoader(); }

/** * Will be called on stop. */ public void onStop() { log("onStop"); }

public static void testClassLoader() { log("ContextClassLoader: " + Thread.currentThread().getContextClassLoader()); }

public static void log(String text) { try { PrintWriter pw; pw = new PrintWriter(new FileWriter(new File("C:\\TestService.log"), true)); pw.println(text); pw.close(); } catch (IOException ex) { Logger.getLogger(TestService.class.getName()).log(Level.SEVERE, null, ex); } } }