I have a project (Spring) that runs perfectly and runs under Eclipc. All files are located. Technically they are in src / main / resources. I read for example the file in the code like this:

private final String KEY_FILE = "AccessKey.key"; private void setKey() { ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {}); Resource resource = appContext.getResource(KEY_FILE); ((ClassPathXmlApplicationContext) appContext).close(); // работали ради этого serverKey = readFile(resource); } private String readFile (Resource resource) { StringBuilder sb = new StringBuilder(); BufferedReader br = null; String line; try { br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8")); while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally {if(br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); }} return sb.toString(); } 

after exporting the project to jar, the launch leads to the error:

 java.io.FileNotFoundException: class path resource [AccessKey.key] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) 

I suspect when compiling is not set to pass the ClassPath to the jar. tell me where to look.

  • in dogonku. The file in the jar assembly is there and it lies in resources /. If I put it in the root of the assembly - it is read. - Vladimir Galkov
  • look at the maven-plugin and <outputDirectory>. Perhaps this will help you - Aidar Zainutdinov
  • everything does not work correctly - read the file in the root, and put it in resources - keekkenen

1 answer 1

  1. The simplest and most reliable is to launch the command file BAT / CMD or SH to launch the application and to register all the necessary parameters in it, including CLASSPATH.
  2. If you really want to stuff everything inside the JAR, then see here how to include the description of CLASS-PATH in the MANIFEST.MF Adding Classes to the JAR File's Classpath
  3. You can do this in step 2 with the maven Adding Classes to the JAR File's Classpath