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 519389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:02:10+00:00 2026-05-13T08:02:10+00:00

My java app uses JNI to invoke a library written in C. This native

  • 0

My java app uses JNI to invoke a library written in C. This native library logs errors to stderr but I would like to redirect the error stream through my log4j logger somehow. Is this possible?

The C library is external – I don’t have the source, so cannot change it.

Thanks

  • 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-13T08:02:10+00:00Added an answer on May 13, 2026 at 8:02 am

    Note: I have not tried this answer; YMMV.

    The POSIX method freopen will change the underlying file associated with a stream. As the manpage states: “The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout)”.

    So, you could create your own JNI library that simply redirects the stream into a file. However, there are several serious roadblocks to making this work:

    1. You’ll need to ensure that your Java
      program doesn’t itself use standard
      streams, because they’ll be
      redirected too. A workaround for
      this is to change System.out et al
      to something else when your program starts up.
    2. It’s possible that
      the third-party library bypasses the standard
      text streams, and writes directly to
      the underlying file descriptor. It’s
      unlikely but possible. I can’t
      remember whether freopen() just
      changes the file descriptor used by
      the buffered stream, or actually
      re-opens the underlying fd.
    3. You’ll
      still have the problem of connecting
      changes to the “file” to a logger.

    That last point is by far the biggest: stdio writes to an OS-level file descriptor. You would have to create some Java-level code to read that descriptor and write to a log. My first thought is that you’d have to use a named pipe, and then spin up a new Java thread to read that pipe. But that won’t be portable to different operating systems, and will that you manage the pipe name in your program’s configuration.

    Assuming that this is a server program, and does not itself process the standard IO streams, I think your best solution will be to configure Log4J with a console logger, and simply redirect all console output to a file.

    Or talk to the people who wrote the library, to get them to add configurable logging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 299k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here are the steps to deploy ASP.NET Website on Windows… May 13, 2026 at 7:45 pm
  • Editorial Team
    Editorial Team added an answer An alternative to IN is to create a temporary table,… May 13, 2026 at 7:45 pm
  • Editorial Team
    Editorial Team added an answer From the django q object documentation: You can compose statements… May 13, 2026 at 7:45 pm

Related Questions

I need to detect whether my application is running within a virtualized OS instance
I am currently using memcached with my java app, and overall it's working great.
I have two application that need to talk to each other. App1 needs to
I'm writing my first non-trivial Java app that uses: networking a GUI threads It's
I have a java app that uses about 15G on a machine with 16G.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.