TheForbidden
TheForbidden

Reputation: 1573

execute jar without a main?

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?

Upvotes: 5

Views: 8172

Answers (7)

Java42
Java42

Reputation: 7706

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

Upvotes: 3

OldCurmudgeon
OldCurmudgeon

Reputation: 65841

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.

Upvotes: 0

A4L
A4L

Reputation: 17595

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.

Upvotes: 0

darkcrux
darkcrux

Reputation: 146

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

Upvotes: 0

Kjartan
Kjartan

Reputation: 19121

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.

Upvotes: 0

shazin
shazin

Reputation: 21893

There should be atleast one class with a main method

Upvotes: 0

TheEwook
TheEwook

Reputation: 11117

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.

Upvotes: 3

Related Questions