1
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.

Bleach
  • 309
  • 3
  • 18

2 Answers2

0

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

Rainer
  • 761
  • 5
  • 20
  • I do use `org.apache.commons.configuration.XMLConfiguration` and it does not seem to have a constructor having `InputStream`. – Bleach Jun 27 '18 at 11:40
  • Although I think it does have a method called `load` that I could use for `InputStream`. – Bleach Jun 27 '18 at 11:46
  • It seems like you are using an old version (1.1) Please consider using the newer version >=2.0 which has no more File constructor and the load method now is called read. – Rainer Jun 27 '18 at 12:21
0

In Java, the class File represents a file in the OS' filesystem. Something that's inside a jar is not in the OS' filesystem. They cannot be accessed through File.

Use getClass().getResourceAsStream() to read your resources that are in jar/classpath. If your library doesn't support loading stuff from InputStream, upgrade to a version that does.

kumesana
  • 2,495
  • 1
  • 9
  • 10