5

Can I execute a jar file without having a main class, in this way:

java -jar my_jar.jar -getX arg1 arg2 ...

Knowing that I have a method called getX with takes arg1 arg2 ... as arguments. Any idea?

TheForbidden
  • 1,533
  • 4
  • 22
  • 30

7 Answers7

3

You must have a main method.

The signature for main needs to be:

public static void main(String[] args){
    // Insert code here
}

Therefore, you can call your method getX inside the main method.

TheEwook
  • 11,037
  • 6
  • 36
  • 55
3

The short answer is no...but...

Here is a technique you can use to pass a method name and parameters on the command line when invoking the jar. This instructional example will take a method name as the 1st parameter. Tweak as necessary and add error checking/handling as needed.

package com.charles.example;
import java.lang.reflect.Method;

public class MethodRouter {

public void myMethod(final String[] args) {
    System.out.println("Amazing!! " + args[1]);
}

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        args = new String[2];
        args[0] = "myMethod";
        args[1] = "yoyo-yada-bla";
    }
    final MethodRouter methodRouter = new MethodRouter();
    final Class<?>[] types = { String[].class };
    final Class<?> _class = methodRouter.getClass();
    final String methodName = args[0];
    final Method _method = _class.getDeclaredMethod(methodName, types);
    if (_method != null) {
        _method.invoke(methodRouter, new Object[] { args });
    }
}

}

Output:

Amazing!! yoyo-yada-bla
Java42
  • 7,628
  • 1
  • 32
  • 50
  • @rcorbellini If you are asking will changing "types={String[].class};" to "types={args.getClass()};" also work. The answer is yes. I think args will always be String[] in this scenario. But I like your way better. Good tweak. Thanks. – Java42 Mar 27 '13 at 12:09
  • 1
    change "_class.getDeclaredMethod(methodName, types);" to "_class.getDeclaredMethod(methodName, args.getClass());" – rcorbellini Mar 27 '13 at 17:21
0

There should be atleast one class with a main method

shazin
  • 21,379
  • 3
  • 54
  • 71
0

You cannot execute a method just by passing it as an argument. You have to parse your arguments -getX arg1 arg2 witch is passed to your main method in the parameter args, if interpret them yourself to execute the method you want.

If the class with the main method is not in the META-INF/MANIFEST.MF like this

Manifest-Version: 1.0
Class-Path: .
Main-Class: some.pack.age.MainClass

then you can specify it (full qualified) as the first argument.

java -cp my_jar.jar some.pack.age.MainClass -getX arg1 arg2 ...

If there is no class with a main method in a jar then you can't execute it. the JVM needs an entry point to start execution and this is the main method. such a jar is just a library.

A4L
  • 17,353
  • 6
  • 49
  • 70
0

Not sure why you would want this, but you could set up a Unit Test project, and call your methods from it. That should (sort of) let you execute the methods, without an (explicit) Main method.

Your test-runner will still include an (implicit?) Main method though, which will then be the entry-point for the execution of your methods.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

If you're going to execute a jar using "java -jar", you're going to need to define a main class. You'll need a main class that defines a main method for the jar to be executable this way.

You can however, execute a class within a jar (provided they have a main method), using the following syntax:

java -classpath my_jar.jar com.project.MyClass
darkcrux
  • 146
  • 5
0

You could write a proxy jar that dynamically links to your jar and calls methods from it. The proxy jar will have to have a class with main method though.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213