How can I have a command line prompt call a method in Java?
-
I have a method that will print via a printer already. I just need to call it via command line. Thanks!
-
Answer:
I am not sure what exactly you're trying to do, but I think you should learn how to use command line arguments: http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
Adil at Yahoo! Answers Visit the source
Other answers
You can't directly call methods from the command line, since the code is compiled. If it was a language that is only interpreted, such as Python, you would be able to do that by simply typing the name of the method with it's arguments. What you can do is use something such as a java.io.BufferedReader or java.util.Scanner and such to read what the user types into the command line and interpret it. For example: Scanner s = new Scanner(System.in); String line = s.readLine(); if(line.startsWith("myMethod")){ java.util.StringTokenizer st = new java.util.StringTokenizer(line.substring… line.length() - 1), ",", false); String arg0 = st.nextToken(); String arg1 = st.nextToken(); String arg2 = st.nextToken(); myMethod(arg0, arg1, arg2); } What I did: I created a Scanner object, read one line from the command line (System.in). If that line starts with "myMethod", then we assume the user wants to call the method myMethod, which takes 3 arguments.Then we substring the line to get everything in between the parenthesys and make a StringTokenizer with it. Then we separate the tokens by ",", then supply the tokens to the method. Of course, you should handle cases where, for example, the user types too many or too few arguments. Also handle other argument types, like integers, with Integer.parseInt(). EDIT--- If you mean calling the method while the program is NOT running, do the following, assuming you method's name is myMethod() and it takes two arguments. public class MyClass{ public static void main(String[] args){ myMethod(args[0], args[1]); } public static void myMethod(String arg0, String arg1){ //do your stuff here } } Compile it: >javac MyClass.java Then type the following at the command line: >java MyClass
Lucas Kuhlemann
Not enough information, really. If the method is static, and in another class, then use: OtherClass.printItOut(...args...); ...and maybe assign the result. If the method is static and defined in the same class, then you don't need the classname. part: printItOut(..args...); If it is not static, then it's an instance method and you'll have to create an instance of that class and then use that instance to invoke the method: OtherClass anObject = new OtherClass(); anObject.printItOut(..args...); In this case, you probably also have arguments to the constructor, or other method calls, to supply the anObject instance with what to print and/or where to print it.
husoski
import java.util.Properties main() { try { Process P=Runtime.getRuntime().exec("C:\\windows… } catch(IOException E){} } The above will start notepad. Here, we assume notedpad.exe is available in C:\windows directory If you want to run your program, insert its exe file name along with path informatio and argument. Read about processbuilder class and use if needed
James Bond
Not quite sure what you're asking but You can run the .class that contains the main(..) method file from the command line with dir>java myClass
modulo_function
Related Q & A:
- How can I mount a network drive in Mac OS X in Java?Best solution by Stack Overflow
- How can I get a job at Universal in a show?Best solution by universalstudioshollywood.com
- How can I get a full ride scholarship to a college?Best solution by Yahoo! Answers
- How can i send a animated graphic picture to a cell phone?
- How can i start a handbag line?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.