javaxmlxml-configuration

XMLConfiguration Size is 0


File file = new File( "justskiphere" );
  if ( !file.exists() )
  {
     file =
           new File( Thread.currentThread().getContextClassLoader()
                 .getResource( "general_cat_column_order.xml" ).getFile() );
     LOG.info( "Found in the thread" );
  }
  XMLConfiguration config = null;
  try
  {
     config = new XMLConfiguration( file );
     LOG.info( config.getBasePath() );
     LOG.info( config.getFileName() );
     LOG.info( config.getRootElementName() );
     LOG.info( "" + config.getRootNode().getChildren().size() );

  }
  catch ( final ConfigurationException e )
  {
     TableColumnHelper.LOG.warn( "Could not find the xml file.", e );
  }

Hello, When I try to read from jar, It does see the file is there, but does not get the contents of it. So the result of LOG up there is:


12:58:33,665 [main] [INFO] TableColumnHelper - Found in the thread    
12:58:33,701 [main] [INFO] TableColumnHelper - /home/mert/Desktop/inspector-1.0-3/file:/home/mert/Desktop/inspector-1.0-3/groundstation.jar!        
12:58:33,701 [main] [INFO] TableColumnHelper - general_cat_column_order.xml        
12:58:33,701 [main] [INFO] TableColumnHelper - configuration        
12:58:33,701 [main] [INFO] TableColumnHelper - 0        
12:58:33,702 [main] [INFO] TableColumnHelper - Items in set: 0

I do not understand why. The jar is: general cat column order is the file in the jar and it has a content in it.

What is the reason?

Thank you in advance.


Solution

  • Reading files from within jars in always something I have to think about ;-)

    I always end up using

    getClass().getClassLoader().getResourceAsStream(...)
    

    The "..AsStream" part is important, since the file object cannot look inside a jar. I am not sure which XMLConfiguration you use, but it most likely has a InputStream-constructor.

    you may also wanna have a look at how to read a file from a jar file