0

I am now developing a java web application. I use some library (.jar) file and create some java under source package (.java). In my .java file, I make some codes for reading an external file and also the jar file that I used will read external file too called from my .java file.

I don't get any problem when running as java application, but I got an error when creating an object in my servlet. An error messages say that my .java file and the .jar file could not find my needed external files. I add my external files directly in my project folder.

To overcome this, I tried : 1.Add my external files into my lib folder. And I still failed. 2.Using project properties to add on packaging (right click on project then select compile and select packaging). I add all of them there. And I still failed. All the error that I got after doing the point 2 is :

WARNING: StandardWrapperValve[analyzer]: PWC1382: Allocate exception for servlet analyzerjava.lang.NullPointerException
at alphabeta.Alpha.loadAlpha(Alpha.java:36)
at alphabeta.AlphaBeta.loadCorpus(AlphaBeta.java:111)
at alphabeta.AlphaBeta.<init>(AlphaBeta.java:93)
at alphabeta.Analyzer.init(Analyzer.java:28)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1444)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1071)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

And also when I deploy my project, the glassfish log show me this :

WARNING: Illegal character in path at index 14: file:/D:/Grade 4/Noble 2/Finish II/AlphaBeta/AlphaBetaSystem/build/web/WEB-INF/lib/alphagamma.jar java.net.URISyntaxException: Illegal character in path at index 14: file:/D:/Grade 4/Noble 2/Finish II/AlphaBeta/AlphaBetaSystem/build/web/WEB-INF/lib/alpahgamma.jar
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3086)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.<init>(URI.java:595)
at java.net.URL.toURI(URL.java:936)

Here is my code for reading an external file. I implement this on java source code that will be called in servlet when I create its object.

 public void loadPaper() throws FileNotFoundException, IOException {
    File[] corpus = new File(getDirectory()).listFiles();
    System.out.println(corpus.length);
    for (int iPaper = 0; iPaper < corpus.length; ++iPaper) {
        File paper = corpus[iPaper];
        BufferedReader input = new BufferedReader(new FileReader(paper));
        StringBuilder contents = new StringBuilder();
        String line;
        while ((line = input.readLine()) != null) {
            contents.append(line).append("\n");
        }
        String[] rawContent = contents.toString().split("\n\n");
        Paper cPaper = new Paper(iPaper, rawContent[0], rawContent[1], rawContent[rawContent.length - 1]);
        contents = new StringBuilder();
        for (int iContent = 2; iContent < rawContent.length - 1; ++iContent) {
            contents.append(rawContent[iContent]).append("\n\n");
        }
        cPaper.setText(rawContent[0] + "\n\n" + contents.toString());
        this.getCorpusCollection().add(cPaper);
        input.close();
    }
}

The directory is a property for this class. I set it when I want to create its object. Thank you.

kidright
  • 13
  • 1
  • 9

3 Answers3

0

Reading external file in Java is often painful... It all depends on which method you're using. You can:

  • use absolute path (may require configuration)
  • use the classLoader
  • use a library to help you (like want Spring provide for example)

Could you paste the reading code ?

You have to be aware that launching your code inside a Java EE container is very different from using it from CLI, in terms of class-loading and relative paths.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Feugy
  • 1,898
  • 14
  • 18
0

I had a similar problem when I did a project that used external sources (.jar). The thing is that when you run the application with netbeans or eclipse it knows how to load at run-time the external sources (.jars). When you export your application there are some things that you have to take into consideration:

  • export the external sources in your source
  • add the path of the external source to the classpath of the created application so it will know how to run it at runtime

The idea is that the external sources have to be loaded at runtime (something that netbeans and eclipse do automatically) .

Calin Andrei
  • 186
  • 2
  • 5
0

Finally I could figure it. Okay, the trick that I use is: I tried to save my a file from java class where the class that want to read the data. At there, I print the absolute path to find out, where the server save the external path as relative path to my application. Based on that information, I put all my external file there and well my aplication goes well.

Thank you for the help.

kidright
  • 13
  • 1
  • 9