Get classpath for current running thread



http://www.coderanch.com/t/603661/java/java/classpath-current-running-thread

void displayClassPath() {
 
     ClassLoader classloader = Thread.currentThread().getContextClassLoader();
 
     do {
         URL[] urls = ((URLClassLoader)classloader).getURLs();
 
         for(URL url: urls){
             System.out.println(url.getFile());
         }
         classloader = (URLClassLoader)classloader.getParent();
 
     } while(classloader != null);
 }



get system class path
  public static void main (String args[]) {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        URL[] urls = ((URLClassLoader)cl).getURLs();

        for(URL url: urls){
        System.out.println(url.getFile());
        }
        
   }

List all files in the classpath or in a Jar
List<File> list = FileUtils.getFiles(System.getProperty("java.class.path"));
    for (File file: list) {
      System.out.println(file.getPath());
    }
   
    list = FileUtils.getFiles(System.getProperty("sun.boot.class.path"));
    for (File file: list) {
      System.out.println(file.getPath());
    }
    list = FileUtils.getFiles(System.getProperty("java.ext.dirs"));
    for (File file: list) {
      System.out.println(file.getPath());
    }

阅读更多

[Thread]IllegalMonitorStateException : current thread not owner

08-29

//程序清单如下:rnrn//: c01:SingletonPattern.javarn// The Singleton design pattern: you canrn// never instantiate more than one.rnrn// Since this isn't inherited from a Cloneablern// base class and cloneability isn't added,rn// making it final prevents cloneability fromrn// being added through inheritance:rnrnfinal class Singletonrnrn //private static Singleton s = null;rn private int i;rn private static int referenceCount;rn private final static int referenceMax = 4;rn rn private Singleton(int x)rn rn i = x;rn rn rn public static synchronized Singleton getReference()rn throws ReferenceOverAgeExceptionrn rn Singleton s = null;rn rn if ( referenceCount < referenceMax )rn rn s = new Singleton(1);rn referenceCount ++;rn rn elsern rn throw new ReferenceOverAgeException("References are over age.");rn rn rn return s;rn rn rn public static synchronized Singleton getReference(long timeout)rn throws ReferenceOverAgeExceptionrn rn while ( true )rn rn long startTime = new java.util.Date().getTime();rn rn tryrn rn return getReference();rn rn catch (ReferenceOverAgeException e)rn rn Thread currentThread = Thread.currentThread();rn rn tryrn rn currentThread.wait(timeout); rn rn catch (InterruptedException ie)rn rn ie.printStackTrace();rn rn rn long endTime = new java.util.Date().getTime();rn rn if ( (endTime - startTime) >= timeout )rn rn throw new ReferenceOverAgeException("References are over age.");rn rn rn rn rn rn public int getValue()rn rn return i;rn rn rn public void setValue(int x)rn rn i = x;rn rnrnrnrnfinal class ReferenceOverAgeException extends RuntimeExceptionrnrn public ReferenceOverAgeException(String msg)rn rn super(msg);rn rn rn public ReferenceOverAgeException()rn rn rnrnrnrnpublic class SingletonPatternrnrn public static void main(String[] args)rn rn tryrn rn Singleton s1 = Singleton.getReference();rn System.out.println("s1");rn System.out.println("s1.i = "+s1.getValue());rn rn Singleton s2 = Singleton.getReference();rn s2.setValue(2);rn System.out.println("s2");rn System.out.println("s1.i = "+s1.getValue());rn System.out.println("s2.i = "+s2.getValue());rn rn Singleton s3 = Singleton.getReference();rn s3.setValue(3);rn System.out.println("s3");rn System.out.println("s1.i = "+s1.getValue());rn System.out.println("s2.i = "+s2.getValue()); rn System.out.println("s3.i = "+s3.getValue()); rn rn Singleton s4 = Singleton.getReference();rn s4.setValue(4);rn System.out.println("s4");rn System.out.println("s1.i = "+s1.getValue()); rn System.out.println("s2.i = "+s2.getValue()); rn System.out.println("s3.i = "+s3.getValue()); rn System.out.println("s4.i = "+s4.getValue()); rn rn Singleton s5 = Singleton.getReference(100);rn s5.setValue(5);rn System.out.println("s5");rn System.out.println("s1.i = "+s1.getValue()); rn System.out.println("s2.i = "+s2.getValue()); rn System.out.println("s3.i = "+s3.getValue()); rn System.out.println("s4.i = "+s4.getValue()); rn System.out.println("s5.i = "+s5.getValue());rn rn catch (ReferenceOverAgeException e)rn rn e.printStackTrace();rn rn rn tryrn rn // Can't do this: compile-time error.rn // Singleton s3 = (Singleton)s2.clone();rn rn catch(Exception e)rn rn e.printStackTrace(System.err);rn rn rn ///:~rnrnrn// 运行后结果如下:rns1rns1.i = 1rns2rns1.i = 1rns2.i = 2rns3rns1.i = 1rns2.i = 2rns3.i = 3rns4rns1.i = 1rns2.i = 2rns3.i = 3rns4.i = 4rnException in thread "main" java.lang.IllegalMonitorStateException: current threarnd not ownerrn at java.lang.Object.wait(Native Method)rn at org.javaresearch.jfml.designpatterns.Singleton.getReference(SingletonrnPattern.java:59)rn at org.javaresearch.jfml.designpatterns.SingletonPattern.main(SingletonPrnattern.java:132)rnrnrn// 请帮忙看看,问题出在哪里?如何解决?谢谢

No Session found for current thread

02-09

在spring3.1整合hibernate4的时候出现No Session found for current thread,求大神看看是什么原因?rn[code=html]HTTP Status 500 - No Session found for current threadrnrntype Exception reportrnrnmessage No Session found for current threadrnrndescription The server encountered an internal error that prevented it from fulfilling this request.rnrnexceptionrnrnorg.hibernate.HibernateException: No Session found for current threadrn org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)rn org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024)rn com.hrmsys.dao.impl.BaseDaoImpl.getSession(BaseDaoImpl.java:20)rn com.hrmsys.dao.impl.BaseDaoImpl.findByProperty(BaseDaoImpl.java:117)rn com.hrmsys.dao.impl.UserDaoImpl.findByUsernameAndPassword(UserDaoImpl.java:16)rn com.hrmsys.service.impl.UserServiceImpl.validateUser(UserServiceImpl.java:24)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn java.lang.reflect.Method.invoke(Unknown Source)rn org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)rn org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)rn org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)rn $Proxy15.validateUser(Unknown Source)rn com.hrmsys.action.UserAction.login(UserAction.java:30)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn java.lang.reflect.Method.invoke(Unknown Source)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)rn org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)rn org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)rn org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)rn[/code]rn这是applicationContext.xml里的sessionFactory里的代码rn[code=html]rn rn rn rn rn rn rn falsern org.hibernate.dialect.SQLServerDialectrn truern falsern org.springframework.orm.hibernate4.SpringSessionContextrn rn rn rn rn [/code]

没有更多推荐了,返回首页