1

I have java code corresponding to a policy mining algorithm in a repository and have to execute this using commands on the git bash terminal (Using a Windows PC). I have generated test data and stored it in a file (named temp_check), local to the above mentioned repository. I need help in writing a python program that executes the mining algorithm using the generated input on the git bash terminal on Windows. While the commands mentioned in the bash_script.sh run successfully when entered manually, they throw an error when called through the python program.

PS:- The java code is publicly available and an implementation of a paper. I just need to use this code as a black box in order to get the corresponding output

I have pasted below the contents of the files I have written till now and the corresponding error messages I got.

Python Script

import os
import subprocess

os.environ['PATH'] = 'C:/Program Files/Java/jdk-15.0.1/bin/java.exe' + os.environ['PATH']

bash_command = "./bash_script.sh"
subprocess.run(["bash", "-c", bash_command])

bash_script.sh

#!/bin/bash

# Compile
./compile.sh

# Run using the input file
./run -m ../case-studies/temp_check.abac -verbose >> temp_output.txt

compile.sh (Already present in the repo)

#!/bin/bash
if [ ! -e "bin" ];
then
    mkdir bin
fi

cd src/

if [ "${OS}" = "Windows_NT" ]; then
# on Microsoft Windows, use semicolon as a separator in classpath.
javac -cp "../lib/commons-math-2.2.jar;../lib/commons-math3-3.3.jar;." -d ../bin edu/dar/util/*.java
javac -cp "../lib/commons-math-2.2.jar;../lib/commons-math3-3.3.jar;." -d ../bin edu/dar/algo/*.java
else
# on other OSs, use colon as a separator in classpath.
javac -cp "../lib/commons-math-2.2.jar:../lib/commons-math3-3.3.jar:." -d ../bin edu/dar/util/*.java
javac -cp "../lib/commons-math-2.2.jar:../lib/commons-math3-3.3.jar:." -d ../bin edu/dar/algo/*.java
fi;

run (Already present in the repo)

#!/bin/bash

cd bin

java -cp "../lib/commons-math-2.2.jar;../lib/commons-math3-3.3.jar;." edu.dar.algo.Experiment $* 

cd ..

The folder structure is as follows:

case-studies/
   -- temp_check.abac
lib/
   -- commons-math-2.2.jar
   -- commons-math3-3.3.jar
src/
bin/
compile.sh
run
python_script.py
bash_script.sh
temp_output.txt

When the python script is run, I get the following error: Error: Could not find or load main class edu.dar.algo.Experiment Caused by: java.lang.ClassNotFoundException: edu.dar.algo.Experiment

But there is no error thrown when commands are entered manually on the gitbash terminal. Thanks in advance.

12
  • BTW, bash -c ./bash_script is needlessly starting two separate copies of bash. It'd be much more sensible to just run ./bash_script Commented Sep 28, 2023 at 21:14
  • And $* is buggy. Always use "$@" with the quotes to pass your argument list through to a child process; otherwise entries in that list get split on spaces and glob-expanded. Commented Sep 28, 2023 at 21:16
  • Also, think about taking out the if [ ! -e "bin" ] and just using mkdir -p bin || exit unconditionally -- that way you always try to create a bin directory if it doesn't exist, and if you fail abort the script. Commented Sep 28, 2023 at 21:17
  • And PATH should contain the directory that java.exe is in, not the path of the java.exe file itself. Commented Sep 28, 2023 at 21:17
  • Beyond that -- I'd suggest having your run script check whether edu/dar/algo/Experiment.class exists and abort with an explicit error message otherwise. Commented Sep 28, 2023 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.