2

I'm opening bat file using processing executable file. all it does is start Exe2.exe and then looks for files until they are created. that bat is located in a specific folder which also has Exe2.exe. When I open it by double clicking, it works as intended but whenever I use

try {
      p1 = r.exec("cmd /c start " + Path + "/open.bat");
    }
      catch(Exception c) {
    }

it opens bat file and I get error "Windows cannot find Exe2.exe" and in console this is what it runs C:\Users\syste\Downloads\processing-3.5.4> start Exe2.exe

the starting path is different from where open.bat file is. It's start from where processing is saved. If there's a way to have it start from correct folder or somehow pass the path with processing. I don't want to hardcode path into it manually since i want it to run on different computers

open.bat content:


start Exe2.exe

cls

@ECHO OFF

SET LookForFile="answer.txt"

:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt

GOTO CheckForFile


:FoundIt
echo the number is:
more answer.txt

del "answer.txt"

GOTO CheckForFile

simple sketch to reproduce:

String Path;
Runtime r = Runtime.getRuntime();
Process p1;

void setup(){
   Path = sketchPath()+"/lib";
   try {
    p1 = r.exec("cmd /c start " + Path + "/open.bat");
  }
  catch(Exception c) {
  }

}

There's a folder called "lib" inside sketches folder which has open.bat and Exe2.exe files

FinalBos5
  • 25
  • 4
  • Would it be possible to post a minimal sketch (and ideally) the file structure you plan to use ? It's a bit tricky to reproduce otherwise and it may lead to many wrong assumptions in understanding/solving the problem. (You can use the `tree` command (remember the `/F` flag prints files and folder and `/A` uses ASCII character (in case formatting looks strange on stackverflow)). Assuming you eventually want to export the sketch as a .exe, you should be able to place the .bat and exe2.exe files in the data folder and use `dataPath`. – George Profenza Jun 19 '22 at 22:16
  • Bare in mind, once you launch open.bat, the context what the current directory may be lost and pointing to exe2.exe may not work. (`%cd%` and `pushd`/ `popd` should help with that). You should also post the contents of open.bat. – George Profenza Jun 19 '22 at 22:18
  • updated it with open.bat content and simple sketch to reproduce it – FinalBos5 Jun 19 '22 at 22:34
  • Thank you for the update. If I got this right, let's say you have a sketch named ExeLauncherSketch, it would contain a lib folder which in turn contains open.bat and exe2.exe ? Did I get this right ? – George Profenza Jun 19 '22 at 22:45
  • Yes that is correct – FinalBos5 Jun 19 '22 at 22:55
  • 2
    Try changing `start Exe2.exe` to `start "%~dp0Exe2.exe"`, which should prefix the `.exe` name with the **d**rive and **p**ath of the batch file. – Magoo Jun 19 '22 at 23:03
  • It did execute the Exe2.exe but in a weird way. Exe2.exe shouldve created a text file but instead it launched a python console. – FinalBos5 Jun 19 '22 at 23:23
  • I removed quotation marks from "%~dp0Exe2.exe" and it works fine now. thanks – FinalBos5 Jun 19 '22 at 23:39
  • A bit off-topic, but would exe2.exe do something that could be programmed in Processing directly ? (I'm curious because the contents of open.bat can be programmed in Processing: even though the syntax might be slightly lengthier, it would simplify the launch processes(just one exe, no crazy path hoops to jump through, launching 2 more commands),etc.). – George Profenza Jun 19 '22 at 23:40
  • I'm using neural network library written in python. – FinalBos5 Jun 19 '22 at 23:43
  • The reverse applies: you can use Python's [subprocess](https://docs.python.org/3/library/subprocess.html) module to launch open.bat (via `subprocess.call` / `subprocess.check_output` / etc.). I suspect the Processing sketch needs to do something more complex too, right ? (If so, remember that if it's simply drawing shapes on basic interation there are other python packages that can help (e.g. pyprocessing (a bit outdated), pygame, kivy, etc.) – George Profenza Jun 20 '22 at 06:35

1 Answers1

2

Based on your comment, using a file structure like this:


C:.
│   ExeLauncherSketch.pde
│
└───lib
        exe2.cmd
        open.bat

Where exe2.cmd is a placeholder for exe2.exe:

@echo "exe2 placeholder script"
timeout 10

You could do something like:

void setup() {
  String path = sketchPath("lib");
  
  try {
    exec(path + "/open.bat", path);
  }catch(Exception ex) {
    ex.printStackTrace();
  }
  
  exit();
}

This is a workaround where you pass the path to the sketch from the processing sketch to open.bat, you can read/use as the 1st command line argument (%1)

which would make open.bat:

@ECHO "path received "
start %1/Exe2.cmd

cls

@ECHO OFF

SET LookForFile="%1/answer.txt"

:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt

GOTO CheckForFile


:FoundIt
echo the number is:
more answer.txt

del "%1/answer.txt"

GOTO CheckForFile

Update Magoo's answer is what I was looking for (but couldn't remember): a way to get the path where the file is located.

This would simplify the sketch to:

void setup() {
  try {
    exec(sketchPath("lib/open.bat"));
  }catch(Exception ex) {
    ex.printStackTrace();
  }
  
  exit();
}

and swapping %1 with %~dp0:


@ECHO "path received "
start %~dp0Exe2.cmd

cls

@ECHO OFF

SET LookForFile="%~dp0answer.txt"

:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt

GOTO CheckForFile


:FoundIt
echo the number is:
more answer.txt

del "%~dp0/answer.txt"

GOTO CheckForFile

(Remember there's a difference between start and call (more info here))

Update The lib folder is a special folder. When you export an application, the Processing .jar files usually live there which means your .bat/.exe files from the original sketch might not make it to the exported application.windows64 folder. You will need to manually copy the .bat/.exe files to the lib folder after the export process completes. May advice is to rename the lib folder to data in the sketch (and update the path to open.bat in the sketch (e.g. exec(sketchPath("data/open.bat")); (or maybe even exec(dataPath("open.bat")); ): this way you'd have a cleaner structure with .bat/.exe files on a folder and .jar files in another)

George Profenza
  • 50,687
  • 19
  • 144
  • 218