-1

One of my test case is failing with " java.io.FileNotFoundException: /home/utility/abc.xsd (No such file or directory)" exception but that file exist in that path and even that file has read access for all the level ( -rw-r--r-- )

But the same is working fine in windows.

public void test1() {
   URL schUrl = getClass().getClassLoader().getResource("/home/utility/abc.xsd"); //At this line error is occuring
}

I tried adding file.read() , file.exist() and for both its showing false.

java.io.FileNotFoundException: /home/utility/abc.xsd (No such file or directory)
at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileInputStream.<init>(FileInputStream.java:101)
        at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
        at java.net.URL.openStream(URL.java:1048)
        at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:274)
        at com.ares.ocg.util.xml.sax.SchemaImpl.init(SchemaImpl.java:258)
        at com.ares.ocg.util.xml.schema.SchemaProvider.getSchema(SchemaProvider.java:144)
        at com.ares.ocg.util.xml.schema.TestSchema.testSchemaElement(TestSchema.java:57)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at junit.framework.TestCase.runTest(TestCase.java:168)
        at junit.framework.TestCase.runBare(TestCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:243)
        at junit.framework.TestSuite.run(TestSuite.java:238)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

Please , suggest how can i resolve this issue.

vikash srivastava
  • 393
  • 2
  • 4
  • 16
  • https://stackoverflow.com/questions/2593154/get-a-resource-using-getresource – hinotf Sep 06 '19 at 09:25
  • I doubt that the code and the error message fit to each other. You are loading a _resource_ which result in either a correct `URL` instance or `null`. In no way could this code give a `FileNotFoundException`! – Seelenvirtuose Sep 06 '19 at 09:42
  • `getResource("/home/utility/abc.xsd")` is for accessing a resource on the classpath. It is relative to the classpath of your application, it doesn't open the file `/home/utility/abc.xsd` on your filesystem. – Mark Rotteveel Sep 06 '19 at 10:36
  • It just doesn not happen to me: both in Windows 10 and CentOS 7, with and without the file... Are you sure you are not forgetting some important detail? – Sterconium Sep 06 '19 at 14:37

1 Answers1

0

It is always suggestible to use File.separator instead of a slash, because every OS have its own style of a directory structure.

Please refer to this

Dinesh K
  • 269
  • 2
  • 8
  • I already tried that as well but it didn't helped and in linux when i am trying to find the file using 'cd' with the path there in error i can see file exist there. – vikash srivastava Sep 06 '19 at 09:22
  • can you try this command `cat /home/utility/abc.xsd` and check for the file existence – Dinesh K Sep 06 '19 at 09:28