0

Can anyone please help me .

I have one java file that calls C++ code (which interacts with hardware device, i.e. a scanner to login into the application, that means on login window user does not have to enter username/password, just have to scan the badge). I am launching the java through .bat file in Windows 10. My java program is a JavaFx project. It does have a menu item as "Switch User", "Logout". That means if the user clicks on "Switch User", then application should exits and restart should happen (i.e. the login window).

Now When I launch the .bat file first time, the java is called perfectly and it calls my .dll file (which has C++ code) and scans the badge perfectly. If I click on Switch User, the java application exits, but it does not re-launch again. I can see the re-launch command in the command window, but it hangs from there.

Note: This working fine in Windows 7. At this point of time, I don't have control over the C++ code. But if I required, I can request the concerned team.

Here is my code to call the C++, .dll

ScanReader.java

import com.sun.jna.Library;
import com.sun.jna.Native;

public class ScanReader {
    private File dllFile;

    public interface LoadDLL extends Library {
        LoadDLL INSTANCE = (LoadDLL) Native.loadLibrary("ScanReaderCPP", LoadDLL.class);
        int readCard();

    }

    public void loadDll(String name) throws IOException {
        InputStream in = ScanReader.class.getResourceAsStream("/" + name);
        byte[] buffer = new byte[1024];
        int read = -1;
        if (in == null ) return;
        dllFile = new File(new File(System.getProperty("java.io.tmpdir")), name);
        FileOutputStream fos = new FileOutputStream(dllFile);

        while ((read = in.read(buffer)) != -1) {
            fos.write(buffer, 0, read);
        }
        fos.close();
        in.close();
        System.setProperty("jna.library.path", dllFile.getParent());
    }

    public String readCard() {
        try {
            loadDll("ScanReader.dll");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        LoadDLL loadDLL = LoadDLL.INSTANCE;
        cnum = loadDLL.readCard();

        dllFile.delete();
        loadDLL = null;     


        return String.valueOf(cnum);
    }
}

RelaunchUtil.java

public class RelaunchUtil {
    public static void restart() throws IOException {
        try {
            final String launchCommand = "C:\\MY_BATCH_FILES\\my_project.bat";
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {
                        Runtime.getRuntime().exec(launchCommand);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            System.exit(-1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IOException("Error while trying to restart the application", e);
        }
    }
}

ScanReaderCPP.cpp

int ScanReaderCPP::readCard() {

HANDLE ghSemaphore;
DWORD dwWaitResult;
BOOL bContinue = TRUE;
SK.
  • 1,390
  • 2
  • 28
  • 59
  • Check your system logs to see if something went awry with your shutdown hook. Do you have sufficient permissions? Are you certain the shutdown hook runs? If your batch file simply writes to a file, does that work? – technomage Jan 20 '18 at 06:06
  • I found that the C++ code always tries to read card even after a successful read. that is why it is not release the java.exe. So when I restart the client again, since there is already a java process running, it does not restart. So I a global variable and checking a good read. once it find s a good read from reader, come out of the loop (which reads). – SK. Jan 22 '18 at 17:09

0 Answers0