14

so suppose I have a java package....

it's got the main class with the main method

and then it's got a whole bunch of other classes.....

my question is, is it possible to get the args that was passed into the main method from these other classes that are not part of the main class but in the same package...

kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171

5 Answers5

11

No, not portably, there may be some trickery based on the JVM implementation but I've never seen it, and it would be a very bad idea to rely on it even if it existed.

If you want those values elsewhere, the main function needs to make them available somehow.


An easy way to do this (not necessarily the best way) is to simply store away the strings as the first thing in main and provide a means for getting at them:

Scratch2.java:

public class Scratch2 {
    // Arguments and accessor for them.

    private static String[] savedArgs;
    public static String[] getArgs() {
        return savedArgs;
    }

    public static void main(String[] args) {
        // Save them away for later.

        savedArgs = args;

        // Test that other classes can get them.

        CmdLineArgs cla = new CmdLineArgs();
        cla.printArgs();
    }
}

CmdLineArgs.java:

public class CmdLineArgs {
    public void printArgs() {
        String[] args = Scratch2.getArgs();
        System.out.println ("Arg count is [" + args.length + "]");
        for (int i = 0; i < args.length; i++) {
            System.out.println ("Arg[" + i + "] is [" + args[i] + "]");
        }
    }
}

And, when run with the arguments a b c, this outputs:

Arg count is [3]
Arg[0] is [a]
Arg[1] is [b]
Arg[2] is [c]
nickb
  • 59,313
  • 13
  • 108
  • 143
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • why an object of `Scratch2` class is not needed for calling `getArgs()` method – Kasun Siyambalapitiya Apr 18 '17 at 10:44
  • find bugs gives me an error saying `expose internal representation by returning a array`. I clone the array, but still the same error comes – Kasun Siyambalapitiya Apr 18 '17 at 11:03
  • what if mylib.jar is being used by the main.jar (which contains the main class), and mylib.jar is being developed by another group that doesn't want to depend on a common.jar (that might be used by both main.jar and mylib,jar for a Util class that contains args (set by Main class in main.jar)). i.e. this solution cannot be generalized. – yerlilbilgin Sep 23 '17 at 19:39
  • @yerlilbilgin: you may just as well ask "What if you have coding standards that prohibit accessing arguments outside of main?" or "What if none of your colleagues accept your pull request for such a change?". My answer solves the *technical* aspect of getting access to the arguments, which is what the question asked for. If you want to solve the problem of technically arbitrary limitations, it probably should be asked as a separate question, and probably on a different site as it pertains to workplace relations rather than coding per se. – paxdiablo Sep 24 '17 at 02:34
9

The system-properties on some (?) JRE-implementations provide the system-property "sun.java.command" to get the programm-name and parameters that were used to start the program. Like "myjar.jar param1 param2 ...".

While this value doesn't even belong to the set of properties that are mentioned in the documentation, it is present in both Oracle-JRE v1.8 and OpenJRE v1.8 (tested).

I couldn't find any documentation whether this value is supported by default though (best I could find was the list in the System#getProperties() docs). Any clarification on this would be welcome. Handle with care!!!

  • @yerlilbilgin I'm glad you like my answer :D. Nevertheless it's a rather unsafe approach to solving this problem, as it's not standardized (AFAIK) and may work in most cases, but needn't. If you'd rather be on the safe-side, my recommendation would be to use [@paxdiablos answer](https://stackoverflow.com/a/5061386/4668606). Still, it's a nice hack :) –  Sep 22 '17 at 22:38
  • No. I still prefer your answer. There are some cases where I need to read cmd line arguments but its in a library that is used by the main library. I don't want to create a 'common utility class' that is used by both libraries and set args there. So as far as I am not too much multi-os (current project is Windows only), I prefer your answer. – yerlilbilgin Sep 23 '17 at 19:38
6

If you don't care about OS portability, read /proc/self/cmdline or the equivalent for your OS.

See http://en.wikipedia.org/wiki/Procfs

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

As paxdiablo said, your main class would have to store these parameters and then either distribute or make available to needed ones. Often a good idea would be to let another class do the parsing of these parameters, and provide an object of this class instead of the raw command line to whoever needs it.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
0

I'm kind of a newb at this, but you should be able to store the string[] args to a private instance variable, then make an accessor method for it. E.g.,

public class test {
private String[] myArgs = new String[10];
public static void main(String[] args) {
myArgs = args;
}
public String[] getArgs() {
return myArgs;
}
}

Not sure if it will work, but that's the idea.

damurdock
  • 31
  • 1
  • 4
  • The only portable way. Sadly, you must control the main class, and must be able to set the runtime to use it. – Dragas Mar 07 '23 at 12:48