Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I invoke a java method other than main(String[]) from the command line?

Tags:

java

methods

Can I invoke a java method other than main(String[]) from the command line?

like image 833
Inversus Avatar asked Nov 08 '11 17:11

Inversus


People also ask

How do you execute a specific function in Java?

To call a method in Java, write the method's name followed by two parentheses () and a semicolon; The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method.

How do you call a jar file from the command line?

We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.

How do you call a method in the main method?

Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System. out. println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"


7 Answers

If you don't have a main function, you can just add one, and if you do, you can just add a series of if-then blocks to the top.

public static void main(String[] args){
    if (args[0].equals("MY_METHOD"))
        callMyMethod();
    else if(args[0].equals("MY_OTHER_METHOD"))
        callMyOtherMethod();
    //... Repeat ad nauseum...
    else {
        //Do other main stuff, or print error message
    }
}

Then, from the command line:

$ java [MyPackage.]MyClass MY_METHOD

Will run your method.

This is pretty hackish - I'm almost sure it's not what you want to do, but hey, it answers the question, right?

like image 133
FrankieTheKneeMan Avatar answered Oct 03 '22 19:10

FrankieTheKneeMan


If you install a REPL for a JVM language (Groovy probably takes the least work to get started with), then you can invoke Java methods at the REPL prompt (Groovy's is called groovysh). groovysh has some odd features (my least favorite bit is that declaring variables with def doesn't do what you'd think it should) but it's still really useful. It's an interesting feature that Groovy doesn't respect privacy, so you can call private methods and check the contents of private variables.

Groovy installs include groovysh. Download the zip file, extract it somewhere, add the location of the bin directory to the path and you're good to go. You can drop jars into the lib folder, for the code you're running and libraries used by that code, and Groovy will find them there.

like image 34
Nathan Hughes Avatar answered Oct 03 '22 17:10

Nathan Hughes


Here is a bash function that lets you do just that:

function javae {
  TDIR=`mktemp -d`
  echo "public class Exec { public static void main(String[] args) throws Exception { " $1 "; } }" > $TDIR/Exec.java && javac $TDIR/Exec.java && java -cp $CLASSPATH:$TDIR Exec;
  rm -r $TDIR;
}

Put that in ~/.bashrc and you can do this:

javae 'System.out.println(5)'

Or this:

javae 'class z { public void run() { System.out.println("hi"); } }; (new z()).run()'

It's a hack of course, but it works.

like image 32
Gavin Haynes Avatar answered Oct 03 '22 17:10

Gavin Haynes


You cannot invoke even the main method from the command. The JVM invokes the main method. Its just a convention. It always needs to be "public static void main".

What is your use case?

like image 27
Pavan Avatar answered Oct 03 '22 17:10

Pavan


From The Java Virtual Machine Specification

The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

So main appears to be special.

like image 21
ccoakley Avatar answered Oct 03 '22 18:10

ccoakley


No you cant

As per Java command line FAQ (which is dead now.) You can check Java Threads FAQ

The entry point method main() is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly designed to avoid clashes with the Thread start() and Runnable run() methods, for example.

Check the FAQ. You will get some good knowledge about JAVA command line

like image 30
bilash.saha Avatar answered Oct 03 '22 18:10

bilash.saha


No, I don't think so. main() is the entry point. That's defined by the language. You can wrap a script around the main() call ("java MyApp arg1...argn"), of course, to obscure the name (and even hide that you're using Java) and to provide your own parameter syntax and parsing -- that is a capability provided by the OS, of course, through some sort of command-line scripting language.

If you use Java to create other types of executables, like Applets or GWT applications, then the entry point is different, but I think you're thinking specifically about executables run from the command line.

like image 22
Steve J Avatar answered Oct 03 '22 19:10

Steve J