This question already has answers here:
How do I parse command line arguments in Java?
(21 answers)
Closed 2 years ago.
I am coding this below functionality where I need to run a class which takes some parameters this will perform an action on the server that is given in the url ....
oracle.ucm.client.DownloadTool --ping --URL=abz.com --param1=abc --param=xyz
And this class is present in a jar which I have included in the build path.
Executing this manually through command line looks something like this:
bash$ java -classpath ".jar file location" abc.xyz.DownloadTool --ping --url=www.google.com --username=xyx --password=abc
Please let me know how to program this.
Convert your JAR file to executable. Executable name can be "ucmclient". Ref: https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
ucmclient oracle.ucm.client.DownloadTool --ping --URL=abz.com --param1=abc --param=xyz
In your main class:
Retrieve class name from command line arguments:
String className = args[0];
Create object of class using class name, You can use any of below option:
Use switch case
switch(className)
{
case "oracle.ucm.client.DownloadTool"
oracle.ucm.client.DownloadTool downloadTool = new oracle.ucm.client.DownloadTool();
downloadTool.main(args);
}
You can use Reflection.
You can also use factory design pattern.
Related
This question already has an answer here:
is there any function in java which behaves like getopt from c
(1 answer)
Closed 5 years ago.
I am writing a java program that will be run from the command line and where the user should be able to indicate their preferences like this, for example:
The user wants to send from the Client to the Server their name (n) and weight (k), and they'll set the Server to have a window (w)of 4 and a delay (d) of 50%...so the commandline would look something like this:
(java abc.Client -n Roger -k 400 receiver_ip_addr receiver_port java
abc.Server -w 4 -d 0.5 receiver_ip_addr receiver_port)
Everything I look up on UI from the commandline mentions reading with Scanner, like: "what is your name?" followed by: name = Scanner.nextLine();
Thanks in advance for any help!
Check the description of the java main method:
https://docs.oracle.com/javase/tutorial/getStarted/application/index.html
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information >to your application. For example:
java MyApp arg1 arg2
So you just need to iterate through the arguments and read them.
This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 6 years ago.
Lets say i have this String: String run = "System.out.println\(\"Hello\"\)". What i want to do is run what is in the string to output Hello in console.
Maybe there is a method like String.run()?
Try BeanShell , build your app with jar library.
import bsh.Interpreter;
private void runString(String code){
Interpreter interpreter = new Interpreter();
try {
interpreter.set("context", this);//set any variable, you can refer to it directly from string
interpreter.eval(code);//execute code
}
catch (Exception e){//handle exception
e.printStackTrace();
}
}
Maybe in Java 9 you could use the REPL but as it's not there yet You would need to
* create a temporary file with a class with a know to You API
* run javac on it and compile it
* load the compiled class with a class loader
* run the code
If You want to do is running dynamically defined scripts in Your code then You could use Nashorn and JavaScript. It would do what You want. Also You could use Groovy in your project instead of Java - the syntax is similar to Java but Groovy is a dynamic language.
No, you cannot do it and there's no method to run this command in String. Anything withing the double quotes becomes String literals only and compiler doesn't take care of any command written in that.
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 9 years ago.
Im a java noob. Basically I am trying to create a program that compares two command line arguments while ignoring case and prints out the lesser of the two strings. Here is what I have so far:
public class CompareStrings
{
public static void main(String[] args)
{
String s1 = new String(args[0]);
String s2 = new String(args[1]);
if ( s1.compareToIgnoreCase(s2) > 0 )
System.out.println(s2);
else if ( s1.compareToIgnoreCase(s2) < 0 )
System.out.println(s1);
else
System.out.println("Both strings are equal.");
}
}
I keep getting the error
Error: Could not find or load main class CompareString
when I try to run it. What am I doing wrong?
"Error: Could not find or load main class CompareString"
Your error message says you couldn't load class "CompareString", but your code says your class name is CompareStrings.
Your class name is wrong.
Your error says
Error: Could not find or load main class CompareString
but the name of class is CompareStrings not CompareString
launch using java CompareStrings
Read this good tutorial on compiling and launching java programs
First of all you are trying to use wrong class, should be CompareStrings and not CompareString.
Second, I would recommend using a nice utility lib for handling command line called Cliche from Google Code site
And third, it would be good to check if the given string is null before you call compareToIgnoreCase on it
as per your error i can gues that you are running CompareString but your class is CompareStrings
So either run java CompareStrings or rename your class to CompareString
If this is the only code you have, save that file as CompareStrings.java not CompareString, and in command prompt javac CompareStrings.java. (to do this you need to configure java). then use java CompareStrings abc cbs to run this. and this will give you out put as abc
This question already has answers here:
Java execute command line program 'find' returns error
(2 answers)
Closed 10 years ago.
I am trying to execute a find command using java code. I did the following:
sysCommand = "find . -name '*out*' > file1"
Runtime runtimeObj = Runtime.getRuntime();
try {
Process processObj = runtimeObj.exec(sysCommand);
processObj.waitFor();
...
This Linux command is executed when I use command line but fails in Java, why?
As far as I know, it is not allowable to use any form of piping operator in Runtime.exec. If you want to move the results to a file, you will have to do that part in Java through Process.getInputStream.
If you are interested in doing this in Java then you will want to do something like this:
public void find(File startDirectory, FileFilter filter, List<File> matches) {
File[] files = startDirectory.listFiles(filter);
for (File file:files) {
if(file.isDirectory()) {
find(file, filter, matches);
} else {
matches.add(file);
}
}
}
Then you need but write the FileFilter to accept directories and files that match your pattern.
This question is probably a duplicate or a duplicate.
Anyway, you could use File.list, providing a Filter on the type
of files you want. You could call it recursively to get all sub-directories. I don't love this answer. You would think there is a simpler way.
A friend of mine recommended Commons-Exec from Apache for running a command. It allows you to use a time out on the command. He recommended it because Runtime can have issues with large stdout and stderr.
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.