简体   繁体   English

Jenkins中没有此类File异常,但在本地运行良好

[英]No such File exception in Jenkins, but runs fine locally

I have a JUnit test file for my code that reads an xml file and converts it to string: 我的代码有一个JUnit测试文件,该文件读取xml文件并将其转换为字符串:

String xml = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\testfile.xml")));

The test runs and passes locally, but when I run a Jenkins build, it fails with java.nio.file.NoSuchFileException: src\\test\\resources\\testfile.xml 该测试在本地运行并通过,但是当我运行Jenkins构建时,它将失败并显示java.nio.file.NoSuchFileException:src \\ test \\ resources \\ testfile.xml

Do I need to change my file path when pushing? 推送时是否需要更改文件路径?

you should use resource as stream for this: 您应该为此使用资源作为流:

InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
if (is != null) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String xml = reader.lines().collect(Collectors.joining(System.lineSeparator()));
}

(code can be improved a lot ... but it will give you a direction) (代码可以改进很多……但是它将为您提供指导)

(check out try by resource to handle streams) (签出按资源尝试处理流)

Alright, so I figured out my problem and I feel pretty silly. 好了,所以我发现了问题,觉得很傻。 When Java builds on my windows machine, using \\ to seperate files is fine; 当Java在Windows机器上构建时,使用\\分隔文件就可以了; however when Jenkins builds, files need to be seperated with / 但是,当詹金斯(Jenkins)生成时,文件必须用/分隔

ie my file names should be 即我的文件名应该是

src/test/resources/testfile.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有gradle的黄瓜应用程序在本地运行良好但在Jenkins上失败 - Cucumber app with gradle runs fine locally but fails on Jenkins 我的程序在Eclipse中运行良好,但抛出作为jar文件运行的异常 - My program runs fine in Eclipse but throws exception running as a jar file 批处理文件错误-线程“ main”中发生异常,但无需批处理文件即可编译并正常运行 - BATCH file error - Exception in thread “main” but compiles and runs fine without batch file .Jar在Windows上运行正常,但在MAC上引发异常 - .Jar runs fine on Windows but throws exception on MAC 我的程序抛出异常,但运行正常 - My program is throwing an Exception,but it runs fine cassandra单元测试在Jenkins上运行但在本地工作时发生ExceptionInInitializerError - ExceptionInInitializerError when cassandra unit tests runs on Jenkins but works locally 代码在IDE中可以正常运行,但不能作为Jar文件运行 - Code runs fine in IDE but not run as Jar file 程序可以在IDE中正常运行,但不能作为Jar文件运行 - Program runs fine in IDE but not as Jar file Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行 - Java program runs fine in Eclipse but not as a .jar file 如果测试运行正常,为什么会收到反射异常NoSuchMethodException - Why do I get a Reflection exception NoSuchMethodException if the tests runs fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM