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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:32:19+00:00 2026-05-11T20:32:19+00:00

While searching for how to do this, I found some vague discussion about different

  • 0

While searching for how to do this, I found some vague discussion about different options, like JNI vs JNA, but not much in the way of concrete examples.

Context: if Java’s File.renameTo() cannot do it’s job (for whatever reason; it is a little problematic), I’d like to fall back to directly using this native Windows function, which is defined in kernel32.dll (from this answer):

BOOL WINAPI MoveFile(
  __in  LPCTSTR lpExistingFileName,
  __in  LPCTSTR lpNewFileName
);

So, using whatever approach, how exactly would you call that function from within Java code? I’m looking for the simplest way, with the minimum amount of non-Java code or extra steps (e.g. in compilation or deployment).

  • 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-11T20:32:19+00:00Added an answer on May 11, 2026 at 8:32 pm

    If you go with JNA, consider invoking MoveFileW directly – it saves having to provide configuration info to choose between Unicode and ANSI calls.

    import java.io.*;
    import com.sun.jna.*;
    
    public class Ren {
    
      static interface Kernel32 extends Library {
        public static Kernel32 INSTANCE = (Kernel32) Native
            .loadLibrary("Kernel32", Kernel32.class);
    
        public static int FORMAT_MESSAGE_FROM_SYSTEM = 4096;
        public static int FORMAT_MESSAGE_IGNORE_INSERTS = 512;
    
        public boolean MoveFileW(WString lpExistingFileName,
            WString lpNewFileName);
    
        public int GetLastError();
    
        public int FormatMessageW(int dwFlags,
            Pointer lpSource, int dwMessageId,
            int dwLanguageId, char[] lpBuffer, int nSize,
            Pointer Arguments);
      }
    
      public static String getLastError() {
        int dwMessageId = Kernel32.INSTANCE.GetLastError();
        char[] lpBuffer = new char[1024];
        int lenW = Kernel32.INSTANCE.FormatMessageW(
            Kernel32.FORMAT_MESSAGE_FROM_SYSTEM
                | Kernel32.FORMAT_MESSAGE_IGNORE_INSERTS, null,
            dwMessageId, 0, lpBuffer, lpBuffer.length, null);
        return new String(lpBuffer, 0, lenW);
      }
    
      public static void main(String[] args) throws IOException {
        String from = ".\\from.txt";
        String to = ".\\to.txt";
        new FileOutputStream(from).close();
        if (!Kernel32.INSTANCE.MoveFileW(new WString(from),
            new WString(to))) {
          throw new IOException(getLastError());
        }
      }
    }
    

    EDIT: I’ve edited my answer after checking the code – I was mistaken about using char[] in the signature – it is better to use WString.

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

Sidebar

Ask A Question

Stats

  • Questions 178k
  • Answers 178k
  • 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 MyContext.Orders will fetch the orders from the database. Since your… May 12, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer Just a guess-- it could be that their server has… May 12, 2026 at 3:37 pm
  • Editorial Team
    Editorial Team added an answer How to Script User and Role Object Permissions in SQL… May 12, 2026 at 3:37 pm

Related Questions

I need to sort an array containing a list of words and search the
I'd like to create a hotkey to search for files under a specific folder
I have been here before, asking for a mapping library that could store objects
I am currently porting a project with a few hundred code files and dependencies

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.