1

I am building my Java project with Maven and I have a script file that ends up in the target/classes/resources folder. While I can access the file itself via this.getClass.getResource("/lookUpScript.py").getPath(), I cannot execute a shell command with "." + this.getClass.getResource("/lookUpScript.py").getPath(); this ultimately ends up being ./lookUpScript.py. To execute the shell command I am using a method that is part of my company's code that I can get to work fine with any command not involving a file. Is there a standard way of accessing files located in the resources area of a Maven build that may fix this?

4
  • Is the script executable? Is it headed with #!/ usr/bin/python? Is there an error that is printed or does nothing happen? Commented Jul 27, 2016 at 20:26
  • Yes, it is executable and I can execute it myself outside of the program. When I deploy my application to a company server I get errors related to the script file not being found Commented Jul 27, 2016 at 20:29
  • Try this.getClass().getClassLoader().getResource("/lookUpScript.py").getPath() If that doesn't work, try taking a look at the path that is being generated when run on the server. Commented Jul 27, 2016 at 20:35
  • 2
    You literally cannot access a resource that is inside a JAR file as a File or a Path. It will never work. What you must do is have an InputStream to the file getClass().getResourceAsStream(...), copy that to a temp file (Files.createTempFile and File.copy) and execute that temp file. Commented Jul 27, 2016 at 21:52

1 Answer 1

1

The maven path for all the artifacts is not the same that gets generated when you run it or export the project. You can check this by exporting the project as Jar/War/Ear file and viewing it via winRAR or any other tool.

The resources should be in jar parallel to com directory if its a jar project, but you can double check it.

Sign up to request clarification or add additional context in comments.

1 Comment

I like to add this is not really maven specific. Resources in the classpath are not files. If you want to run a phyton script its interpreter will need to be able to read the file, but it is inside a jar. One would either need to copy it first from the jar into a directory and execute it from there or package it outside the classes or resources folder - if creating a war file just somewhere in webapp/WEB-INF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.