相关疑难解决方法(0)

在生产中的播放框架中从公共文件夹中读取文件

我的播放应用程序是2.4.2.在开发人员模式中,我曾经使用Source.fromFile("./ public/files/abc.json")从后端控制器的公用文件夹中读取文件

当我将应用程序转换为生产模式时,我收到了File Not Found Exceptions.我发现公用文件夹在生产模式下打包在资产jar中.我能做什么才能在开发和生产模式下工作?

playframework playframework-2.4

8
推荐指数
1
解决办法
5749
查看次数

使用docker容器中的java访问类路径中的JSON文件时遇到FileNotFoundException(SprintBootApplication)

我在使用docker容器加载Java jar类路径中的JSON文件时遇到FileNotFoundException,它是一个Spring-Boot应用程序.此JSON文件在资源文件夹中可用.我能够在./target/classes/path下的docker中看到JSON文件.

Resource resource = resourceLoader.getResource("classpath:folderNm/file.json");
HashMap<String, String> headerMapping = (HashMap<String, String>) parser.parse(new FileReader(resource.getFile().getAbsolutePath()));
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个例外:

java.io.FileNotFoundException: class path resource [folderNm/file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/folderNm/file.json
Run Code Online (Sandbox Code Playgroud)

我试过了

- > resource.getFile().getPath(); - > resource.getFile().getCanonicalPath(); - >" ./target/classes/folderName/fileName"(硬编码的FilePath位置) - >" /app.jar!/folderNm/file.json"(硬编码的FilePath位置)

InputStream inputStream = getClass().getResourceAsStream("xyz.json");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = br.readLine()) != null)
                responseStrBuilder.append(inputStr);
Run Code Online (Sandbox Code Playgroud)

以上都没有运行.请建议一种解决此问题的方法.

java linux json docker spring-boot

7
推荐指数
1
解决办法
2772
查看次数

Java:如何从不同的模块中读取文件?

我有一个包含多个maven模块的项目

project/pom.xml
       /external_services/pom.xml
                         /ifs/pom.xml
                             /src/test/java/
                                           /MockIFSClient.java
                                           /IFSClientTest.java
                             /src/test/java/resources/sample.json
       /inventory/pom.xml
                 /business/pom.xml
                             /src/main/java/InventorySummary.java
                 /services/pom.xml
                             /src/main/java/InventorySummaryResource.java
                             /src/main/test/InventorySummaryResourceTest.java
Run Code Online (Sandbox Code Playgroud)

MockIFSClient访问sample.json

try {
    inventoryPriceDetails = mapper.readValue(new File(getClass().getResource("/getInventoryAndPriceResponse.json").getPath()), new TypeReference<List<InventoryPriceDetail>>() {
    });
} catch (final IOException e) {
    throw new RuntimeException("could not read resource :" + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

所以IFSClientTest运行鳍,因为它们在同一个包中.

问题?尝试访问相同代码的
InventorySummaryResourceTest调用MockIFSClient,但现在它失败了

could not read resource :file:/Users/harith/IdeaProjects/inventory_api/external_services/ifs/target/ifs-1.0-SNAPSHOT-tests.jar!/sample.json (No such file or directory)
Run Code Online (Sandbox Code Playgroud)

services/pom.xml具有依赖性

<dependency>
    <groupId>com.org.project.external_services</groupId>
    <artifactId>ifs</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我做了什么改变

new File(getClass().getResource("/getInventoryAndPriceResponse.json").getPath())
Run Code Online (Sandbox Code Playgroud)

这样它也可以从不同的模块访问

java file maven

5
推荐指数
1
解决办法
7688
查看次数

如何从aws lambda java中的类路径读取文件

当我尝试执行我的 aws lambda function

1) Error injecting constructor, java.lang.NullPointerException
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:66)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.class(ServicesResource.java:56)
while locating in.nikhilbhardwaj.path.route.resources.ServicesResource
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction.<init>(HelloWorldIntentAction.java:44)
while locating in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction
while locating in.nikhilbhardwaj.path.alexa.intent.IntentAction annotated with @com.google.inject.multibindings.Element(setName=,uniqueId=10, type=MAPBINDER, keyType=java.lang.String)
at in.nikhilbhardwaj.path.alexa.intent.IntentModule.configure(IntentModule.java:17) (via modules: in.nikhilbhardwaj.path.alexa.AlexaStarterApplicationModule -> in.nikhilbhardwaj.path.alexa.intent.IntentModule -> com.google.inject.multibindings.MapBinder$RealMapBinder)
while locating java.util.Map<java.lang.String, in.nikhilbhardwaj.path.alexa.intent.IntentAction>
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl.<init>(IntentHandlerServiceImpl.java:16)
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerService
for parameter 0 at in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet.<init>(AlexaStarterSpeechlet.java:26)
while locating in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.initializeServiceIds(ServicesResource.java:88)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:68)
at in.nikhilbhardwaj.path.route.resources.ServicesResource$$FastClassByGuice$$3d6e91ec.newInstance(<generated>)
Run Code Online (Sandbox Code Playgroud)

我已经classpath按照 …

java classpath aws-lambda

5
推荐指数
1
解决办法
2865
查看次数

使用 getResourceAsStream 在 Java 中不起作用

我有一个非常简单的方法,它使用该getclass().getResourceAsStream()方法来读取文件。但是它总是返回 null,我无法弄清楚出了什么问题。这是我的一段代码。

InputStream sw = getClass().getResourceAsStream("/filename.txt");
BufferedReader bf = new BufferedReader( new InputStreamReader(sw));
Run Code Online (Sandbox Code Playgroud)

sw始终保持为空。该文件filename.txt存在于我的项目的根目录中。

编辑:我找到了原因。我意识到我是从 Eclipse 运行我的项目,并且该项目不是我 PC 上类路径的一部分。但是,如果我将程序打包为 jar 文件然后运行它,则 jar 文件中的文件被视为资源,可以使用 getResourceAsStream() 方法读取。

java

2
推荐指数
2
解决办法
1万
查看次数

日食和路径

您好我在Eclipse下编写了这样的函数:

public static ArrayList<String> getMails(){
    ArrayList<String> mails = new ArrayList<String>(); 
      try{
            FileInputStream fstream = new FileInputStream("mails.txt");
            DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null)   {
              mails.add(strLine.trim());
            }

            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
            }

    return mails;
}
Run Code Online (Sandbox Code Playgroud)

mails.txt文件位于workspace/projectname下,我想将此项保存在workspace/projectname/bin /目录下,作为相对路径,因此每当我将workspace/projectname // bin目录复制到其他位置或计算机时,让它工作.但是,当我尝试这个时,我得到"FileNotFound"异常.我怎样才能解决这个问题 ?谢谢

java eclipse path classpath

1
推荐指数
1
解决办法
315
查看次数

java.io.FileNotFoundException:in.txt,(系统找不到指定的文件)

我收到以下错误

java.io.FileNotFoundException: in.txt, (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at FileTest.main(FileTest.java:50)
Run Code Online (Sandbox Code Playgroud)

我确定我已经在src,bin和root目录下创建了一个in.txt文件.我也尝试在主参数中指定完整目录,但仍然无法正常工作.为什么Eclipse不接受它?

import java.io.*;
public class FileTest {
    public static void main(String[] args) {

        try {
            String inFileName = args[0];
            String outFileName = args[1];
            BufferedReader ins = new BufferedReader(new FileReader(inFileName));
            BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter outs = new PrintWriter(new FileWriter(outFileName));

            String first = ins.readLine(); // read from file
            while (first != null) {
                System.out.print("Type in …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
3万
查看次数

在tomcat webapplication中打开一个文件

我想打开一个文件并返回其内容.虽然它与想要打开文件的类在同一目录中,但找不到该文件.如果你能帮助我解决这个问题会很酷.

这是代码:

@GET @Produces("text/html") @Path("/{partNO}/") @Consumes("text/html")
public String getPartNoResponseHTML(@PathParam("partNO") String parID) throws WebApplicationException {
PartNoTemplate partNo = getPartNoResponse(parID);

String result = "";

try {
  result = readFile(PART_NO_TEMPLATE_FILE);
} catch (FileNotFoundException e) {
  e.printStackTrace(System.out);
  return e.getMessage() + e.toString();
  // throw new WebApplicationException(Response.Status.NOT_FOUND);
} finally {
  result = result.replace("{partNO}", parID);
  result = result.replace("{inputFormat}", partNo.getFormat().toString());
}

return result;
}
Run Code Online (Sandbox Code Playgroud)

我猜它找不到文件,因为它在tomcat上运行.我也在使用Jersey和JAX-RS.谢谢您的帮助,

马克西

java tomcat file jersey

0
推荐指数
1
解决办法
1万
查看次数