running java main class using subprocess.Popen in python - java

I want to execute java main class main.java by python using subprocess.Popen(). main.java takes 3 args.
I wonder how to do it? For example I have a HelloWorld.java class:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!" + args[0]);
} }
I tried to call it in python using the command:
print (subprocess.Popen('java C:/Users/testing/Hello.Main sayHello',
shell=True, stdout=subprocess.PIPE).stdout.read())
where 'sayHello' is the string args I want to pass in. It said
Error: Could not find or load main class C:.Users.testing.Hello.Main
Thanks

You may run your java file with extension .class the following way:
java your.path.Main arg1 arg2
where,
java - command, which runs the Java interpreter
your.path.Main - full name of your class (without .class)
arg1 arg2 - the arguments (written by spaces or between ")
Further, when you formatted this line, it transmits in subprocess.Popen() as argument.
subprocess.Popen('java your.path.Main arg1 arg2')
I'm not Python programmer, because I advice you to read documentation about this method.

Related

Trying to understand how to create a jar file that opens terminal and takes in a argument

I finished creating a program but I was told that my program
must be a Java application that takes as a command line argument the name of the file."
I understand I can use the jar command in terminal but I don't undestand how you open the terminal and take a file name as a argument. I was wondering if someone could explain what code is required to do this.
Thanks alot.
I tried creating a basic jar file in terminal with the line "jar cvf findOptimalTransport.jar ." but the jar file does not open, I think its because the current implementation takes the users input with a scannar in the code and prints via the terminal. However, this wont work because a terminal window is not opened with this command.
It doesn't have to be a jar file. Command line arguments can be entered from the command line, when you run your application.
Let me give you an example, about how this works. Let's say you have the below simple Java application:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
}
}
That public static void main() is a method; and more specifically the main method of your application which is what is executed when compiled and ran.
To compile and then run it, you type in the command line/terminal:
javac MyApplication.java //this will compile it
java MyApplication //this will run the main method of MyApplication
But what is that parameter in the main method? What is String[] arguments ?
When you run your program, whatever you type after the application name is an argument, of type String and it is stored in the String array String[] arguments (or most commonly String[] args).
What this means, is that, if you execute your application like this:
java MyApplication some_file.txt // Run application with one arg.
You can access that argument like so:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
System.out.println("You entered: " + arguments[0]);
}
}
Output:
Hello World!
You entered: some_file.txt
Note: To run a jar file, you need to navigate to the folder that the jar file is in and from the command line you can run it by typing:
java -jar <jarname>.jar

How to run Java public static void main from Terminal [duplicate]

This question already has answers here:
How to compile and run a java class with a package
(4 answers)
Closed 4 years ago.
Please click this image for the description
Hi guys, so my homework is asking that I run my program on command prompt which would be Terminal for Mac users like me.
How would I run this on Terminal? How to I access bin?
Also for the >java Echo "echo this string"
How can "echo this string" be accessed as arg[0] and arg1? Which is which for arg[0] and arg1???
from https://www.oracle.com/technetwork/java/compile-136656.html the official Oracle techno document
//A Very Simple Example
class ExampleProgram {
public static void main(String[] args){
System.out.println("I'm a Simple Program");
}
}
Compiling the Program
The Java compiler is invoked at the command line on Unix and DOS shell
operating systems as follows:
javac ExampleProgram.java
Interpreting and Running the Program
The Java interpreter is invoked at the command line on Unix and DOS
shell operating systems as follows:
java ExampleProgram
At the command line, you should see:
I'm a Simple Program
If you already have your Echo.java file, compile it with javac Echo.java.
After that, you can run the generated Echo.class with java Echo "echo this string".
How can "echo this string" be accessed as arg[0] and arg1? Which is which for arg[0] and arg1???
you just System.out.print them in your code and that's it.
on how to start your app from the terminal use
java MyClass
if it's a class or
java -jar MyClass.jar
if it's a jar file
and you can add the Sting [] args in your command like this
java MyClass first_arg second_arg ect...
the array is seperated by white spaces and it's just accessed within your main with the args[] array .
here is what your Echo class might look like :
public class Echo
{
public static void main (String [] args )
{
for (String str : args )
{
System.out.print(str +" " );
}
}
}

How to run Java command line script through python to return object?

I have written a java class
public class Uploadfiles {
public static void main(String[] args) throws InterruptedException {
System.out.println(args[0]);
//uploadFile(args[0]);
}
I have converted it to jar by right click project>Export> Runable jar> Package required libraries into generated Jar.
I have run the command to execute through command line
java -classpath JavaSeleniumLibs.jar javaSeleniumClasses.Uploadfiles Test
The output is
Test
I wanted to run the same commandline script through python and i use the method below
import os
def runCommandline():
print os.system("java -classpath JavaSeleniumLibs.jar javaSeleniumClasses.Uploadfiles Test")
I wanted the output "Test" to be printed as when done from commandline. How to do it?
How to return value from the executed Java function? like "The script is successfully executed"? Can I return other objects from the java function to the python os.system command?

How can I pass variables from the command prompt into my java program

Is there a way to read data from the command prompt? I have a java program that relies on 4 input variables from an outside source. These variables are returned to the command prompt after I run a javascript program but i need a way to pass these variables from the command prompt into my java program, any help would be greatly appreciated!
While executing java program pass the parameters and all the parameters should be separated by space.
java programName parameter1 parameter2 parameter3 parameter4
This parameters would be available in your main method argument
public static void main(String[] args){
//This args array would be containing all four values, i.e. its length would be 4 and you easily iterate values.
for(int i=0; i<args.length; i++){
System.out.println("Argument " + i + " is " + args[i]);
}
Follow the link:
Command-Line Arguments - The Java™ Tutorials : https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
shared by #BackSlash.
It has all the content which would help you to clear all your doubts.
The content from the link is quoted below:
Displaying Command-Line Arguments passed by user from command-line to a Java program
The following example displays each of its command-line arguments on a
line by itself:
public class DisplayCommandLineParameters {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
To compile the program: From the Command Prompt, navigate to the directory containing your .java file, say C:\test, by typing the cd
command below.
C:\Users\username>cd c:\test
C:\test>
Assuming the file, say DisplayCommandLineParameters.java, is in the
current working directory, type the javac command below to compile it.
C:\test>javac DisplayCommandLineParameters.java
C:\test>
If everything went well, you should see no error messages.
To run the program: The following example shows how a user might run the class.
C:\test>java DisplayCommandLineParameters Hello Java World
Output:
Hello
Java
World
Note that the application displays each word — Hello, Java and World —
on a line by itself. This is because the space character separates
command-line arguments.
To have Hello, Java and World interpreted as a single argument, the
user would join them by enclosing them within quotation marks.
C:\test>java DisplayCommandLineParameters "Hello Java World"
Output: Hello Java World

Passing on command line arguments to runnable JAR [duplicate]

This question already has answers here:
How do I pass parameters to a jar file at the time of execution?
(5 answers)
Closed 5 years ago.
I built a runnable JAR from an Eclipse project that processes a given XML file and extracts the plain text. However, this version requires that the file be hard-coded in the code.
Is there a way to do something like this
java -jar wiki2txt enwiki-20111007-pages-articles.xml
and have the jar execute on the xml file?
I've done some looking around, and all the examples given have to do with compiling the JAR on the command line, and none deal with passing in arguments.
Why not ?
Just modify your Main-Class to receive arguments and act upon the argument.
public class wiki2txt {
public static void main(String[] args) {
String fileName = args[0];
// Use FileInputStream, BufferedReader etc here.
}
}
Specify the full path in the commandline.
java -jar wiki2txt /home/bla/enwiki-....xml
You can also set a Java property, i.e. environment variable, on the command line and easily use it anywhere in your code.
The command line would be done this way:
c:/> java -jar -Dmyvar=enwiki-20111007-pages-articles.xml wiki2txt
and the java code accesses the value like this:
String context = System.getProperty("myvar");
See this question about argument passing in Java.
You can pass program arguments on the command line and get them in your Java app like this:
public static void main(String[] args) {
String pathToXml = args[0];
....
}
Alternatively you pass a system property by changing the command line to:
java -Dpath-to-xml=enwiki-20111007-pages-articles.xml -jar wiki2txt
and your main class to:
public static void main(String[] args) {
String pathToXml = System.getProperty("path-to-xml");
....
}
When you run your application this way, the java excecutable read the MANIFEST inside your jar and find the main class you defined. In this class you have a static method called main. In this method you may use the command line arguments.

Categories

Resources