0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your time and help. I tried the above but encountered some problems. I quit the above procedure. I'm learning java instead. Again, thank you very much.

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.