简体   繁体   中英

Controlling output file of a java jar using command line

I am using a .jar file, but unfortunatley as a black box, ie I do not know what exactly is in there nor how it all works.

I am sending commands to the Mac terminal from a Python script. I enter the following command:

java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt

This does what I need: analyses input using the 'black box' and creates and new file with analysis output. This new file is created in the folder where jarfile.jar is located.

I want to have this file put somewhere else upon creation.

I have tried using the > operator, specifying a path, eg:

java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt > /output/path/

this created a file in my desired location, but it was simply the message from Terminal, saying "The operation was carried out successfully" - the analysis results file was created in the same folder as before. I tried %*> too, but it threw an error.

As a poor workaround I now have a function, which retrospectively finds and moves all the newly created files (analysis output) to my desired folder.

Is there a way to control the output files with the command line within the original command? Or is it something that is specified somewhere in my jar file? My problem is that editing it is not allowed.

I'm new to python. However, I may suggest to try few things, if they can work for you. Apology me, if does not work! I believe that you have already done the following step:

import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])

Like, if you have a properly configured jar path, then can run jar directly.

Secondly, look at the cwd parameter (is used for executable). Include a cwd param as x

def run_command(command, **x):
    with subprocess.Popen(command,...., **x) as p:

for run_command specify the path of either the working directory (possibly it should be) or the full system path. I'm not sure, just try both.

for outputline in run_command(r'java -jar jarfilepath', cwd=r'workingdirpath', universal_newlines=True):

print(outputline, end='')

Alternatively, you can try to run command from the directory in which you wish to store output file. Try: run the popen as

subprocess.Popen(r'directory of running command', cwd=r'workingdir')

where workingdir could be your current directory path.

If it does not work, try without r' . If still does not work, try doubling slash in the path like ( C:\\\\ abc\\\\def )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM