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 +" " );
}
}
}
Related
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
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?
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
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.
Here is a strange problem I ran into:
I create a single program to print all the received args, Here is the code:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < args.length; i ++) {
System.out.println(args[i]);
}
}
}
Then I built a jar file of it and ran the following command:
java -jar test.jar test&1
However, it didn't print "test&1" as expected. The result of it is:
test
'1'is not recognized as an internal or external command,operable program or batch file.
So my question is: what is the seperation of args? If I really need to receive "test&1", what should I do?
Thanks!
It's nothing to do with Java. The & character is special to the Windows shell (I can tell it's Windows from the error message): It separates two commands on one line, so what you're doing is telling the shell to run java -jar test.jar test and then run 1. If you want to pass test&1 to Java, you'll have to put it in quotes:
java -jar test.jar "test&1"
The & is also special on *nix shells (but in a different way, it runs the command in a sub-shell). There, you could use quotes as above, or put an \ before the & instead. But not on Windows.