Controlling output file of a java jar using command line - java

I am using a .jar file, but unfortunatley as a black box, i.e. 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, e.g.:
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)

Related

Java using relative path instead of absolute path

I am reading in a file in Java and when I use the absolute path it works fine.
File myObj = new File("/Users/aaronmk2/Downloads/demo2/SystemDependencies/src/sample_input.txt");
However, when I try to use the relative path I get a No such file or directory error
File myObj = new File("../sample_input.txt");
When I use my terminal and use nano ../sample_input.txt it opens up the file.
What do I need to add to get the relative path to work?
Java does relative paths just fine. Clearly, then, the 'current working directory' for your java process is not the same as the cwd when you're invoking nano.
You can check the CWD in java. Either way will work:
System.out.println(new File(".").getAbsolutePath());
or:
System.out.println(System.getProperty("user.dir"));
You should find that it is different. The 'cwd' for a java process is the cwd that it was set to by whatever started java. If you're invoking java from the command line, it'll be the directory you're in as you do so. If you are double clicking a jar, it'll be the directory the jar is in. If you're making a windows shortcut, it's the directory listed in the shortcut. Example:
cd /foo/bar
java -jar /bar/baz/hello.jar
In the above example, the cwd is /foo/bar. Not /bar/baz.

Why does my batch file give an error if it has spaces in path?

I am trying to run a .jar file using a .bat file. But my Batch file gives an error when I have spaces in my path.
For e.g.
#echo off
java -jar %~dp0\..\new\myFile.jar %*
This are the contents of my batch file. I know that %~dp0 will give the current drive letter and path to my batch file and then \..\ will go back to it's parent directory and then it will open the new directory and run the jar file from it.
But the problem here is if my path contains a space in it the batch file gives an error:
Error: Unable to access jarfile C:\Users\Vatsal
Assuming my path for .jar file is:
C:\Users\Vatsal Sura\myFolder\new\myFile.jar
and path for my .bat file is:
C:\Users\Vatsal Sura\myFolder\batch\batchFile.bat
Now if I move myFolder directly in C:\, which doesn't contain any spaces in it, then it won't give any error and the .jar file would run as expected.
I want to know why this is happening and what should I do to make spaces count in my path?
Put the offending part in double quotes: java -jar "%~dp0\..\new\myFile.jar" %*
If the expansion is C:\Users\Vatsal Sura\myFolder\new\myFile.jar, then without quotes, -jar is only seeing up to the first space as it's argument (i.e. -jar C:\Users\Vatsal) and the remainder (Sura\myFolder\new\myFile.jar) is a completely different argument not related to the -jar
You need to make the shell/cmd see all of that as one argument. Double quotes will still allow any expansion to happen but everything within the quotes will be seen as a single item (the quotes won't be there when java sees it).
The reason for your problem is simple
Batch Is Simple Too,
Windows needed to create command Line, powershell, blah blah, and with its awesome features. However, Batch only runs on one programmed line of code, yes its confusing lol but when you write the code it is nothing compared to the binary. Batch is not too advanced and therefore does not process spaces when its in the "heart" of the command, like it i do this it literally has the same error however batch can tell the difference:
ec ho
(supposed to be echo)
anyways, a simple solution to your problem can be changing the encoding that your batch file is working on or just simply taking out the spaces on you directory.
Plus, don't use
%~dp0\
(it suKs)
Instead, to simplify this just do
#echo off
java -jar %CD%\new\myFile.jar %*
you don't have to even set %CD% to be anything it just automatically used the current directory.
*even with all this, your initial problem will not be solved. It may if you change the encoding (use notepad++ for that) but i hope you found this answer at least a little helpful to explain how this is working.

Where to put files for a command-line Java program?

I have written a Java program that takes in arguments and then executes. I pass in these arguments from the command line (I am on a Macbook Pro using Terminal, using the bash shell). Let's say the name of my program is prgm. I want to be able to say "prgm " from any directory in the shell and then have that program execute. So I figure I need to write a bash script to reference the Java files and take in arguments, and put that bash script somewhere in my PATH. Where do I put the bash file, and where do I put my Java files? Also, am I right to assume that I only need the .class (binary) Java files?
Step-by-step:
Assuming that the name of the Java executable if myjavaprog.
Assuming that the name of your bash script is myscript.
Make sure myscript is calling myjavaprog using absolute path and the desired arguments.
call echo $PATH and you will see a bunch of paths: /some/path1:/some/other/path2:...
Put your bash script in whatever path you want from the ones returned by echo $PATH.
Go to a random path.
Call you bash script bash myscript. See the execution of myjavaprog.
Tips:
If java program is for personal use only, put it in a path starting with /usr/ or even in your $HOME directory (and add that location to your PATH)
If java program must be shared with other users, put it in an accessible place, so that other users don't need to modify their PATH variable.

executing bash commands from a specific directory

From my application I have to execute an external jar of which I do not have the source.
Given an input file, it processes it, creates an "output" directory and puts in it an mxml output file. Problem is: it creates said directory in tomcat/bin instead of inside the directory of the original file.
Here's what I've tried so far.
Initially
Process p = new ProcessBuilder("java -jar "+newfile.getParent()+"\\converter.jar "+newfile.getPath()+" -mxml").start();
Then, seeing how from console the "output" directory was created in the directory the command was called from, I tried:
String startSim[] = {"cd "+newfile.getParent()+"\\" , "java -jar converter.jar "+newfile.getName()+" -mxml"};
try {
Runtime.getRuntime().exec(startSim).waitFor();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Log non creato.");
}
But with this I get the "file not found" exception for the first instruction. Does anyone know how to possibly solve this problem? I'd like to avoid having to reach for my output file all the way in my tomcat/bin directory.
Thanks for any suggestion!
Paolo
P.s.: by the way, before trying all this I tried simply calling the method I need from the library, but had the same exact problem. So I resolved to execute the jar, instead. And here we are. :)
You can set working directory using ProcessBuilder.directory() method:
ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File("mydirectory"));
pb.command(......);
etc
This does not work for you when you are using Runtime.exec() because cd command is a functionality of shell. You could solve it using this technique but you have to create platform specific command with prefix like cmd /c on windows or /bin/sh on Linux. This way is definitely not recommended.
But in your specific case you do not neither first nor second solution. Actually you are starting one java process from another. Why? you can easily invoke the main() method of the second process directly.
Take a look on META-INF/MANIFEST.mf file from converter.jar. Field Main-Class contains the fully qualified name of main class. Let's say it is com.converters.Main (just for example). In this case you can invoke
com.converters.Main.main(new String[] {newFile.getPath(), "-mxml"});
directly from your code. Just add the jar to your classpath.
Concerning to changing working directory in this case. Check again whether you really need this or your converters.jar supports parameter that does this.
A lazy approach to this may be going to the root directory and descending from there to your tomcat bin directory .

Executing an external executable in a working directory containing spaces in Java?

I have a specific need to unrar files found in different subdirectories during execution of my program on Os x. I do this by calling the freeware command line tool unrar, which works very well.
However, unrar will always unrar files in the current working directory, which means that I have to specify a working directory for the executed process or get every unpacked file in my .jar home folder.
This is easy enough to do using either the processBuilder.directory(dir) or runTime.getRuntime().exec(args,null,dir) for example where dir is a File.
This works excellent but not when the working directory contains a space. As a short example:
File dir=new File("/Users/chargedPeptide/rar 2");
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
Process pr = Runtime.getRuntime().exec(cmd,null,dir);
int exitValue=pr.waitFor();
Will not work, while using: Process pr = Runtime.getRuntime().exec(cmd); instead will launch the command successfully but leave me with all of the files in the jars working directory.
Using processbuilder instead and using processbuilder.directory(dir); to set the directory
exits with the message:
Exception: Cannot run program "/Users/chargedPeptide/rar/unrar" (in directory "/Users/chargedPeptide/rar 2"): error=2, No such file or directory
Help? How do I handle the spaces? I've tried adding backslashes before the spaces to make them literal no help since the File object treats them like actual part of the path.
Edit: To make the whole thing a bit more clear:
1. I have a separate method that feeds the execute method a command and a directory to processbuilder, all directories are found by the previous method and exist. This works except when the dir contains a space.
2.I need to set the working dir or the command will execute in the wrong place.
3.Parsing the file object by:
dir=new File(dir.tostring.replace(" ","\ ");
to put a backslash in front of all spaces does not work since the java File object then looks for a path containing actual backslashes. No luck.
4.Using rt.exec instead of processbuilder dosen't help either.
Any ideas most welcome.
How about:
dir.mkdirs();
before launching the process.
This creates the missing directory.
Edit:
This looks strange.
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar", "2/weather.rar"};
If this was a shell you'd write:
unrar e -o+ "/Users/chargedPeptide/rar 2/weather.rar"
You actually have to put the .rar file in quotes, since otherwise it will be interpreted as 2 arguments to the process.
The way you've split 'cmd' will do exactly that, break the rar argument in two. Try:
String[] cmd = { "/Users/chargedPeptide/rar/unrar", "e", "-o+","/Users/chargedPeptide/rar 2/weather.rar"};
Not sure if it will work, but can you try putting a / at the end of the path.
i.e.,
File dir=new File("/Users/chargedPeptide/rar 2/");

Categories