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?
1 Answer
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.
1 Comment
wemu
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.
#!/ usr/bin/python? Is there an error that is printed or does nothing happen?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.Fileor aPath. It will never work. What you must do is have anInputStreamto the filegetClass().getResourceAsStream(...), copy that to a temp file (Files.createTempFileandFile.copy) and execute that temp file.