Java Spring Boot - Read file inside the . Jar

Asked

Viewed 354 times

1

I’m doing a class that will read an ETL Pentaho Kettle (transformation), I put the file that the class will read in the Resources/KTR folder.

But when I try to run the code as a java application (java -jar), I get an error saying that the file does not exist,

However he is trying to read the local disk file and not from inside my . jar

How do I make him read the file inside the . jar ?

I am using Spring Boot 2.1.0.BUILD-SNAPSHOT and Java 1.8

public class run_tranform {

 public static void main( String[] args ) throws IOException
    {
     String file="src/main/resources/KTR/transformation.ktr";

        try {
            KettleEnvironment.init();
            //TransMeta metaData = new TransMeta(file.getPath());
            TransMeta metaData = new TransMeta(file);
            Trans trans = new Trans( metaData );
            trans.execute( null );
            trans.waitUntilFinished();
            if ( trans.getErrors() > 0 ) {
                System.out.print( "Error Executing transformation" );
            }
        } catch( KettleException e ) {
            e.printStackTrace();
        }
    }

 }
  • 1

    In this directory in Runtime will not really have this file. It will be in classes/KTR. Maybe the best way is something like this: Paths.get(ClassLoader.getSystemResource(resourceName).toURI()).toString(). See if this TransMeta has builder for input stream, maybe it’s better

  • Take a look here

  • @I tried to use it the way I said: Paths.get(Classloader.getSystemResource("KTR/Transformation.ktr").toURI()). toString(); but I’m afraid of null Pointer error when I try to do this.... actually I don’t even need to read the file, just pick up its path inside the jar for the Transmeta read.... so I searched the input stream still does not work with Transmeta

  • @Diego as I said, I recommend you look at this doc TransMeta and see if he has builder by input stream. For path he probably needs a physicist, shouldn’t be able to work with a classpath.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.