Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was thinking of passing the result from Java run from command line into a command line variable. But after searching much for it, I only came across Runtime class which just runs the command.
Therefore, my question, is there anything I can do to get Java result into command line.
Platform of course is Windows.
There are only 2 ways any program (Java or otherwise) can send results back to the "command line":
1) exit code
2) capture the strings output by the program.
#1 can be done in Java using System.exit (or see a couple more options here).
#2 is done by the shell. In Unix, you typically use backticks or $():
OUTPUT=`java app.class`
In Windows Powershell, do something like
$result = & java app.class 2>&1 | Out-String
Possible duplicate of
how-do-i-get-the-result-of-a-command-in-a-variable-in-windows
FOR /F "delims=" %i IN ('java Main') DO set today=%i
Also allows today variable to be set.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to make a java program run numerous times by passing the commands with the different arguments to the Ubuntu terminal, through a .txt. But I have no idea how to do it.
Any suggestion?
I find Karol's response to be great, however another suggestion to show there are many ways to do this on Unix :)
Suppose your class is called JavaClass and arguments stored on .txt
xargs -a .txt java JavaClass
Has same effect than
java JavaClass $(cat .txt)
You could use cat to print the a.txt before calling java:
java $(cat a.txt)
It might be easier to convert a.txt into a bash script:
#!/bin/bash
jargs="-version"
java $jargs
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've searched for similar questions, but despite there where a lot with a similar title all have asked something different.
In my case I want that my java program interact with the terminal (Windows) by writing command and reading the response.
I've found the class ProcessBuilder, but does not seem that do what I want
What I' m searching for is something so:
start the cmd at some given position (like C:\Users\federico)
issue a command (dir or cd desktop); this should not open a command prompt window
read any output that command may result in
and so on, up to the user's exit from the program.
I suspect you simply don't know the English terms for this: what you're looking for is a way to execute commands in the OS, which is called "exec" in every programming language I know of, including Java: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to compile my java code using a bash script. I need to pass in arguments too, which I'm getting from another file in the same directory. My script looks like:
#!/usr/bin/env bash
MYID="$(cat $(pwd)/my.id)"
eval javac src/main/java/foo/bar.java "$MYID"
but it thinks of my argument as another java file for compilation and gives me the error:
Class names, 'abc123', are only accepted if annotation processing is explicitly requested
P.S. abc123 is the only id in the my.id file I need to pass.
Please help me make this work, I have been looking for solutions the whole day. :(
edit: This was a stupid question.
I just realized I was passing in arguments at the wrong place. I figured it out. thankyou!
You are going about this the wrong way. The javac command compiles programs. That is it. It doesn't generate source code.
Based on your comments it sounds like you should be doing something like this:
javac foo/Bar.java
java foo.Bar $(cat my.id) > foo/Baz.java
javac foo/Baz.java
This assumes that foo.Bar is some kind of generator program that knows how to generate java source code.
In reality, I expect that what you are really trying to do is some kind of source-code template expansion. There are simpler and / or more flexible ways to do that that writing a bespoke Java program to do it. (For example, use the "m4" utility.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I’m new to python scripting and I need to install Java using python script . please suggest a method to do this.
Thanks all.
If i am not wrong you are looking for silent installation of Java. Here is a link that explains the same:
Silent installation of Java
Next you would want to run this command using python. For this you can use subprocess in python. Below is a link explaining it:
Using subprocess in Python
You want to display error stream too, so use the subprocess.Popen as:
process = subprocess.Popen(['command plus args as in above link'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
out contains the output of the above command and err contains the error stream.
I would suggest you to install java from command line first, and then use this command in python script.
To install mySql or even tcl, follow similar steps,
1. Find how to install using command line
2. Execute the same command using python
Also, there may already be existing packages to do your job, if so you can use them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there a way to execute a cmd command like "move FolderA FolderB" without creating a .bat file and start it?
It would be nice if it would work without creating files on HDD.
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","move","dirA/a.txt","dirB"});
Process process = new ProcessBuilder("cmd.exe",
"/c","move","dirA/a.txt","dirB").start();
ProcessBuilder is preferred to Runtime.exec() since Java 1.5, according to JavaDoc.
Be sure to read the Process Javadoc to understand how to read from and write to processes.
Shelling out for commands like move is bad practice, because it's neither portable nor secure. Work with File classes instead. But sometimes you have to shell out to interact with more esoteric external programs.