Execute jar file with arguments from c++ and store output - java

I'm developing a project in visual c++ and at some point in code I need to execute some java jar program with some arguments and store it's output into a string
So the code I execute in CMD and it works is:
java -jar weka.jar something.arff
And I get some text returned to the command prompt
I want to do the same thing with c++ and store the output into a string variable
I tried using system function, but as far as I know it's impossible to store output using that one
I also tried using _popen, ( it did work with commands like dir etc...) but it didn't return any output when using the command provided above
Help would be appreciated

Related

Launch powershellscript in java application

Is it possible to run an entire, large, powershell script in a java application without calling it externally by launching a powershell process with the -file parameter? (passing it via the encodedcommand parameter won't work either because of the commandline lenght limitation).
I.e. is there a java library that enables you to paste your powershellscript inside your java app and run it?
I currently embed the powershellscript inside the java application and write it to disk, but I'm looking for a fileless approach.
Since you want to pass a large program, piping from the Java program to the PS script is more suitable than using the command line or environment variables. Since I don't know too much Java and don't have it installed, I'll simulate it from the PS command line in the snippet below. Your Java program would read the lines of the program from a list or some other suitable data structure (whereas this example reads it from the file system). I did see hits on google for piping from Java to an externally executed command.
cat my.ps1|
powershell -command {$scr="";foreach ($line in $input){$scr+=$line+"`n"} echo bxb $s cxc}
After your PS stub has received the program rather than echo it, it would execute it: iex $scr.
If you need to pass parameters from Java to PS you could either pipe them along with the program (for example as lines of code that set global variables) or they could be global variables set in stub code. Other, more complex, variations are possible.

Running Jar files from Python

I want to make a program that can execute jar files and print whatever the jar file is doing in my python program but without using the windows command line, I have searched all over the web but nothing is coming up with how to do this.
My program is a Minecraft server wrapper and I want it to run the server.jar file and instead of running it within the windows command prompt I want it to run inside the Python shell.
Any ideas?
First you have to execute the program. A handy function for doing so:
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
It will return an iterable with all the lines of output.
And you can access the lines and print using
for output_line in run_command('java -jar jarfile.jar'):
print(output_line)
add also import subprocess, as run_command uses subprocess.

How to use java program as a command in cmd?

I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?
Update:
I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.
~Jacob
You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.
Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:
java -jar jarfile.jar param1 param2 ...
So for example, lets's assume that your program takes two arguments. Your script could then be as follows:
java -jar jarfile.jar %1 %2
I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?
With Launch4J you can wrap a Java program in a standalone executable file. I'm not going to copy their (long) feature list here, but definite highlights are the numerous ways presented to customize the resulting exe, its small size, the fact that it's open source and its permissive license that allows commercial usage.

Java Filepath Question

I am trying to finish a java program that uploads a file from a client machine to a webserver. The java program is executed with a bat script. I need to pass in a file name to the java program somehow since the filename is different every time. Or can i somehow use %1 instead of the filepath? I dont know.
Why not simply forward the parameters passed to the shell script to the Java application. I usually do something like this:
#!/bin/zsh
java -jar someapp.jar $#
This will pass all the arguments with which the script was executed to the java app and you can act upon them - as far as I understand you need only only - the file path. I'm not familiar with bat scripts, but I assume they have some similar way of passing args around.
What does the batch file look like that runs the Java program? You can indeed use parameters like this:
java -jar program.jar %1
If you put that line in a file runprogram.bat, then you could run it with:
runprogram somefilename.xyz
and somefilename.xyz will be passed to the Java program as a command line argument.
No they got it, if i could just pass the filepath as a parameter to the executed jar that would be awesome. Just need to figure out how to pass that parameter into a variable in the program....

NSIS - How to run Java Application In NSIS?

I wanna run a java application through NSIS. My java program is called PropertiesReader.java which reads a property from a .config file.How can I do this? Also the output of java program (which in this case is the property value) is stored in some variable say property_value in java program. How can i access back the value of this variable in NSIS script?
From the NSIS srcipt, you can run Exec or ExecWait to run the "java" and pass the PropertiesReader.class as parameter to it. Your java program can write the property_value to a file which you can read back from the script.
Here is a link that has a heading "NSIS and Java". You can check that out.
Just make sure that the PropertiesReader.class file is in the classpath.

Categories