简体   繁体   English

我如何将将由.java文件使用的外部文件放在源包中,并将Java Java Web应用程序中的lib jar放在哪里?

[英]How I put external file that will be used by .java file on source package and lib jar on Java Web Application?

I am now developing a java web application. 我现在正在开发Java Web应用程序。 I use some library (.jar) file and create some java under source package (.java). 我使用一些库(.jar)文件,并在源包(.java)下创建了一些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. 在我的.java文件中,我编写了一些用于读取外部文件的代码,并且我使用的jar文件还将读取从我的.java文件中调用的外部文件。

I don't get any problem when running as java application, but I got an error when creating an object in my servlet. 作为Java应用程序运行时,我没有任何问题,但是在servlet中创建对象时出现错误。 An error messages say that my .java file and the .jar file could not find my needed external files. 错误消息指出,我的.java文件和.jar文件找不到我所需的外部文件。 I add my external files directly in my project folder. 我将外部文件直接添加到项目文件夹中。

To overcome this, I tried : 1.Add my external files into my lib folder. 为了克服这个问题,我尝试了以下操作:1.将外部文件添加到我的lib文件夹中。 And I still failed. 我仍然失败了。 2.Using project properties to add on packaging (right click on project then select compile and select packaging). 2.使用项目属性添加到包装上(右键单击项目,然后选择编译并选择包装)。 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 : 而且,当我部署项目时,glassfish日志向我显示了以下内容:

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. 我在创建Java对象的Java源代码上实现此目标,该源代码将在Servlet中调用。

 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. 谢谢。

Reading external file in Java is often painful... It all depends on which method you're using. 用Java读取外部文件通常很麻烦……这完全取决于您所使用的方法。 You can: 您可以:

  • use absolute path (may require configuration) 使用绝对路径(可能需要配置)
  • use the classLoader 使用classLoader
  • use a library to help you (like want Spring provide for example) 使用一个库来帮助您(例如想要Spring提供)

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. 您必须意识到,就类加载和相对路径而言,在Java EE容器中启动代码与从CLI中使用代码非常不同。

I had a similar problem when I did a project that used external sources (.jar). 当我做一个使用外部资源(.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). 问题是,当您使用Netbeans或Eclipse运行应用程序时,它知道如何在运行时加载外部源(.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) . 这个想法是必须在运行时加载外部源(netbeans和eclipse会自动执行此操作)。

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. 好的,我使用的技巧是:我试图从java类中保存要读取数据的文件。 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. 感谢您的帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM