Sayan Mandal
Sayan Mandal

Reputation: 605

How to run a Java Project in Python?

I have a huge Java Project, which I have to run multiple times and check the output. I am proficient in python, but fairly new to Java and thus I was wondering how to run Java and check outputs in python.

The project file structure is:

MainFolderName
├── .idea
│   ├── artifacts
│   └── libraries
├── lib
│   ├── mathlibrary.jar
│   └── gson-1.2.3.jar
├── out
├── resources
│   ├── pic1.png
│   └── pic2.png
├── src
│   ├── srcfile1
│   │   ├── function1.java
│   │   └── function2.java
│   ├── srcfile2
│   │   ├── anotherfunction1.java
│   │   └── anotherfunction2.java
│   ├── game
│   │   ├── someconfigurations.java
│   │   └── Main.java
│   ├── META-INF
│   └── environment.txt (used in Main.java)
├── config.txt
├── Main.iml
├── setup.json
└── pythonfiletorunMainjava.py

When I try to run the above project using the code:

import os.path,subprocess
from subprocess import STDOUT,PIPE

def compile_java(java_file):
    subprocess.check_call(['javac', java_file])

def execute_java(java_file, stdin):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', '-classpath',  'C:\Program Files\Java\jdk1.8.0_211', # all dependencies are in this folder 
    'parseJason'] 
    proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    stdout,stderr = proc.communicate(stdin)

compile_java('./path/to/Main.java')
execute_java('./path/to/Main.java', 'setup.json')

I get the following error: CalledProcessError: Command '['javac', './path/to/Main.java']' returned non-zero exit status 1.

Anaconda output has some error as:

error: package com.golden.gamedev does not exist 
import com.golden.gamedev.GameLoader;
       ^

and some more:

error: cannot find symbol
    public static Something xyz;
                  ^
  symbol:   class Something
  location: class Main

the "something" is related to packages which don't exist. There are multiple class paths in META-INF.

Thanks for your time. I'd appreciate your help to debug the issue.

edit: In addition to this, is there a way to add string inputs through python? Currently Main.java uses args = new String[] {'some string'} as inputs in main function.

Upvotes: 0

Views: 471

Answers (1)

jigutierrez
jigutierrez

Reputation: 11

Check the project file structure and compare with the package in your Java classes.

MainFolderName
├───Main.java
└───com
    └───test
        └───config
            └───Config.java

Main.java

import com.test.config.Config;

public class Main {

    public static void main(String args[]) {
        Config c = new Config();
        c.printConfig();
    }
}

Config.java

package com.test.config;

public class Config {

    public Config() {}

    public void printConfig() {
        System.out.println("Test Print Config");
    }
}

I think that you could test the Java compile, first in terminal and finally run this in Python program.

Here, an example by args inputs in Python: https://www.tutorialspoint.com/python/python_command_line_arguments.htm

I hope can help you and I'm sorry for my English :P.

Upvotes: 1

Related Questions