Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:55:29+00:00 2026-05-26T10:55:29+00:00

This code runs well in my locally but gives file not found exception when

  • 0

This code runs well in my locally but gives file not found exception when we build using bamboo. Any idea/workaround?

final static String FILE_NAME ="/src/test/java/com/statement/SamplePDFStatementFile.txt";
file = new File(FILE_NAME);
FileInputStream fis = new FileInputStream(file);

I basically want to test a class which reads a file. Here is the Complete Code.

//Main Class
public class PdfRenderer {

    public void render(PdfFile pdfFile) throws IOException {
        FileInputStream fis = new FileInputStream(pdfFile.getFile());

        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.setHeader("Content-Disposition", " attachment; filename=" + pdfFile.getAttachmentName());

        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
            response.getOutputStream().write(buff, 0, bytesRead);
        }

        response.getOutputStream().flush();

    }
}

//Test

public class PdfRendererTest{

    PdfFile pdfFile;
    File file;

    @Test
    public void test_DetailStatementsByAccountNumber() throws Exception {
        new AbstractSeamTest.ComponentTest() {

            PdfRenderer actionBean = new PdfRenderer();

            URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");
            final String FILE_NAME = url.getFile();

            protected void testComponents() throws Exception {

                file = new File(FILE_NAME);

                context.checking(new Expectations() {{
                    one(pdfFile).getFile();
                    will(returnValue(file));
                    one(pdfFile).getAttachmentName();
                    will(returnValue(file.getName()));
                }});

                actionBean.render(pdfFile);
            }

        }.run();
    }
}

I want to set an expectation that pdfFile.getFile() returns SamplePDFStatementFile.txt. If I use getResourceAsStream, am not sure how to convert it to a File Object.

OK.. so now I am using. Looks like thats the answer 🙂

public void inputStreamToFile() throws Exception{
                InputStream inputStream =  this.getClass().getResourceAsStream("/SamplePDFStatementFile.txt");

                file = File.createTempFile("abc","def");
                OutputStream out = new FileOutputStream(file);

                int length = 0;
                byte[] bytes = new byte[1024];

                while ((length = inputStream.read(bytes)) != -1) {
                       out.write(bytes, 0, length);
                }

                inputStream.close();
                out.flush();
                out.close();
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:55:30+00:00Added an answer on May 26, 2026 at 10:55 am

    There are no reliable ways to reference files relative to the project root folder.

    You need to reference this file as a resource instead. As far as I see, you use Maven. If so, you need to put this file into /src/test/resources rather than /src/test/java (perhaps you can also configure Maven to get resources from /src/test/java, but it would be a violation of Maven directory layout convention).

    After that you can load this file as

    InputStream fis = getClass()
        .getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");
    

    Or, if the current class is in the com.statement package, as

    InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code runs well with jQuery-1.3.2.min.js but doesn't run with jQuery-1.6.2.min.js . $(function(){ $(document).mousedown(mouseUpAfterDrag);
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
I have code that runs well on the simulator but as soon as I
I have this code in Python which runs perfectly well. What it does is
This code runs on my local RoR/Windows 7 (64-bit): sql = ActiveRecord::Base.connection() last_pk =
I have a python script the runs this code: strpath = sudo svnadmin create
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self
Please check this code out it compiles and runs absolutely fine.. The question is
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
I have some C++ source code with templates maybe like this - doxygen runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.