4

I am Trying to read and write Extensible Linking Format (ELF) Below is the Line that I am getting Error.

p =  r.exec("./optimizer " + sourceFile + " " + sourceFile + "a" + " " + "--all -i");

After Running this line I am Getting Error Like :

java.io.IOException: Cannot run program "./optimizer": CreateProcess             
error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at GUIMode.GUIMode.actionPerformed(GUIMode.java:213)
Pointy
  • 405,095
  • 59
  • 585
  • 614
FinalYear Nutech
  • 41
  • 1
  • 1
  • 2

3 Answers3

6

There a few possible causes for the "%1 is not a valid Win32 application" message including:

  • the pathname for the application is incorrect,
  • the file named by the pathname is not recognized by Windows as an executable, or
  • the file is a 32 bit executable, but for some reason it is trying to load a 64 bit DLL.

In this case, you are using a relative pathname for the executable, so it is possible that the JVM's current directory is different to what you think ... and the optimizer file is not in that directory.

Try the following:

  1. Replace "./optimizer" with an absolute pathname.

  2. It the command (absolute) pathname or the sourceFile arguments contain spaces, replace the command string with an array of strings; e.g.

    exec(new String[]{
           "C:/path/to/optimizer",
           sourceFile, sourceFile + "a", "--all", "-i"});
    
  3. Try running the command from the command prompt. The idea is to check that this isn't caused by a non-executable file or a DLL problem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Most likely, your code is not running in the directory you think it is.

Use the OP's code given here: how to get current directory in java?

File directory = new File (".");
try {
    System.out.println ("Current directory's canonical path: " 
            + directory.getCanonicalPath()); 
    System.out.println ("Current directory's absolute  path: " 
                + directory.getAbsolutePath());
}catch(Exception e) {
    System.out.println("Exceptione is ="+e.getMessage());
}

Now compare that to where optimizer is located.

Generally speaking, unless you control how the JVM is invoked, you can't rely on relative paths.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
0

I came across the same problem when running a gradle command in windows OS, but other OS ( linux, osx ) are not the problems.

I find the gradle is a bat script in windows OS finally , so need to add the suffix.

    public Process callTestInSubprocess(Class<?> clazz, String testMethod) {
        String canonicalName = clazz.getCanonicalName();
        StringBuffer testCmd = new StringBuffer(isWinOS() ? "gradle.bat" : "gradle")
                .append(" testDebug -Psubprocess=true --tests ")
                .append(canonicalName)
                .append(".")
                .append(testMethod);

        return runSubprocess(testCmd.toString());
    }



    public static boolean isWinOS() {
        return System.getProperty("os.name", "generic").toLowerCase().indexOf("win") != -1;
    }
alps2006
  • 66
  • 4