Simple question on the main method

Hello,
I have a very simple question and hope that somebody 'out there' can help me on this topic:
I just want to pass some Strings to the main method, which means that I want to do sth. like this:
java ProgramXY parameter1, parameter2
I thought it was possible to access these parameters in my main method like the following:
public static void main(String argv[]) {
System.out.println(argv[0]);
System.out.println(argv[1]);
But it does not work. So what do I do wrong ? Thank you for your help,
Findus

That should actually work - though you don't use the comma - just spaces. I don't think a comma would screw things up though - it would just become part of parameter 1.
Of course, you still want to check argv.length to make sure you don't try reading an array member that doesn't exist.
What are you seeing if you're not seeing your parameters?
If you're not putting enough parameters, it MIGHT be possible that you're getting an ArrayIndexOutOfBounds exception before the first System.out.println's are flushed to the screen - unsure though.

Similar Messages

  • Question of the day: In which Thread context does the main method run ?

    let's say, you create a JFrame in the main method:
    public static void main(String[] args)
    new MyJFrame();
    let's suppose, you construct the whole JFrame
    inside the constructor, which contains a lot
    of other Swing components, like JTree's and more.
    AND the JFrame calls setVisible(true) in its
    constructor.
    -> According to my knowledge, this won't work
    properly - it will function, BUT you will get one
    or two quite big exceptions (which usually start
    with a nullpointer exception, produced by the
    PLAF, but the whole stacktrace shows, that the
    the exception was thrown AND catched inside
    the Swing objects.
    Reason for this would be, that all operations
    after the "setVisible(true)" statement must be
    carried out in the eventdispatch thread, according
    to Swing rules.
    Now, as you can see by running the source below,
    the main() method always is processed in special thread,
    NOT the event dispatch thread - which produces the above behaviour.
    My Question:
    Wouldn't it have been easier to let the JVM
    process the main() methods right in the
    event dispatch thread ?
    Or:
    Why is the JVM designed to process the
    main() method in a special thread and not
    the Swing event dispatch thread ?
    opinions?
    Regards
    JPlaz / SnowRaver
    package Test1;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Frame1 extends JFrame
    public Frame1( final String title, int position )
    this.setSize(400,300);
    this.setLocation(position,position);
    this.setVisible(true);
    if( SwingUtilities.isEventDispatchThread() )
    System.out.println("Constructor of "+title+" is processed IN the event dispatch thread.");
    else
    System.out.println("Constructor of "+title+" is processed in ANOTHER than the event dispatch thread.");
    public static void main(String[] args)
    // First creation just from the startup thread context :
    new Frame1("first Frame", 60);
    // Now we let the second creation be done in the
    // eventdispatch thread context for sure :
    SwingUtilities.invokeLater( new Runnable()
    public void run()
    new Frame1("second Frame",120);

    Matt,
    it's been so long, I don't have the full answer in my head.
    This page on Adobe.com has an example that shows Flash 4
    mouse stuff (I'm Mr
    Vague today) when you scroll down. Your timelinme reference
    will be
    something like
    TellTarget(_Root, gotoAndPlay(50))
    Hopefully from that vagueness you can locate the right
    answer.
    Steve
    Adobe Community Expert: Authorware, Flash Mobile and Devices
    My blog -
    http://stevehoward.blogspot.com/
    Authorware tips -
    http://www.tomorrows-key.com

  • How do you call a java class from the main method in another class?

    Hi all,
    How do you call a java class from the main() method in another class? Assuming the two class are in the same package.
    Thanks
    SI
    Edited by: okun on May 16, 2010 8:40 PM
    Edited by: okun on May 16, 2010 8:41 PM
    Edited by: okun on May 16, 2010 8:47 PM

    georgemc wrote:
    To answer your impending question, either the method you're calling has to be static, or you need an instance of that other class to invoke it against. Prefer the latterAnd to your impending question after that: no, don't use the Singleton pattern.

  • How to add a code TestIfElse(10) to the Main method of the Program.cs class?

    How to add a code  TestIfElse(10) to the Main method of the Program.cs class?
    Here is my code and I have copied from the Wiley book for Microsoft certification page 14,
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ifelse_Statement
        class Program
            static void Main(string[] args);
            TestIfElse(10);
            public static void TestIfElse(int n);
            if(n < 10)
                     Console.WriteLine("n is less than 10");
             else if(n < 20)
                    Console.WriteLine("n is less than 20");
             else if(n < 30)
                Console. WriteLine("n is greater than or equal to 30");
    Here is the error list I am getting,
    Error 1      Method must have a return type        C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\switch_Statement\Program.cs
    12 13
    switch_Statement
    Error 2
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\switch_Statement\Program.cs
    12 24
    switch_Statement
    Error 3
    Method must have a return type C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    12 9
    ifelse_Statement
    Error 4
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    12 20
    ifelse_Statement
    Error 5
    Invalid token 'if' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 9
    ifelse_Statement
    Error 6
    Invalid token '10' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 16
    ifelse_Statement
    Error 7
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 16
    ifelse_Statement
    Error 8
    Invalid token '(' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    18 35
    ifelse_Statement
    Error 9
    A namespace cannot directly contain members such as fields or methods
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    20 10
    ifelse_Statement
    Error 10
    Type or namespace definition, or end-of-file expected
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    30 1
    ifelse_Statement
    Error 11
    The type or namespace name 'n' could not be found (are you missing a using directive or an assembly reference?)
    c:\users\pvs\documents\visual studio 2013\projects\lesson01\ifelse_statement\program.cs
    16 12
    ifelse_Statement
    Error 12
    'System.Console.WriteLine(string, params object[])' is a 'method' but is used like a 'type'
    c:\users\pvs\documents\visual studio 2013\projects\lesson01\ifelse_statement\program.cs
    18 26
    ifelse_Statement

    static void Main(string[] args){
    TestIfElse(10);
    public static void TestIfElse(int n)
    if (n < 10)
    Console.WriteLine("n is less than 10");
    else if (n < 20)
    Console.WriteLine("n is less than 20");
    else if (n < 30)
    Console.WriteLine("n is greater than or equal to 30");
    Fouad Roumieh

  • Invoking the main() method of a class within another

    Greetings -
    Can someone tell me what happens when you invoke the main() method of a class from within another class?
    I have a third-party class, designed to be invoked from a command line, that I want to invoke from within a separate class. What appears to be happening is that following the successful execution of the third-party class, the invoking class is terminated as well. That's not the intended behavior I had envisioned. ^_^
    Is there a better way than invoking the main() method? I still need the invoking class to continue executing.
    Thanks!

    Navigate yourself around pitfalls related to the Runtime.exec() method

  • How to notify() the main method in the given code snippet

    consider the given code snippet
    code
    public class WaitTest {
    public static void main(String [] args) {
    System.out.print("1 ") ;
    synchronized(args) {
    System.out.print("2 " ) ;
    try {
    args.wait();
    catch(InterruptedException e){}
    System.out.print("3 ");
    code
    Here since there are no threads to notify the main()method the statement System.out.print("3 "); never gets executed.And i cannot use the non static notify() from a static context.In this case how will I notify the main() method.

    A thread can only do one thing at a time.
    It cannot be waiting and notifying at the same time.
    notify only works if another thread is waiting.
    wait() only stops if another thread notify/notifyAll() it at the same time.

  • A question about the getProperty method defined in the Security class

    Hello Everyone!
    I would like to ask a question about the getProperty method defined in the
    Security class.
    public static String getProperty(String key) Do you know how can I exract the list of all possible keys?.
    Thanks in advance,

    I found the answer, in fact the keys are defined in the java.security file.

  • Need help - how to run this particular method in the main method?

    Hi all,
    I have a problem with methods that involves objects such as:
    public static Animal get(String choice) { ... } How do we return the "Animal" type object? I understand codes that return an int or a String but when it comes to objects, I'm totally lost.
    For example this code:
    import java.util.Scanner;
    interface Animal {
        void soundOff();
    class Elephant implements Animal {
        public void soundOff() {
            System.out.println("Trumpet");
    class Lion implements Animal {
        public void soundOff() {
            System.out.println("Roar");
    class TestAnimal {
        public static void main(String[] args) {
            TestAnimal ta = new TestAnimal();
            ta.get(); // error
            // doing Animal a = new Animal() is horrible... :(                   
        public static Animal get(String choice) {
            Scanner myScanner = new Scanner(System.in);
            choice = myScanner.nextLine();
            if (choice.equalsIgnoreCase("meat eater")) {
                return new Lion();
            } else {
                return new Elephant();
    }Out of desperation, I tried "Animal a = new Animal() ", and it was disastrous because an interface cannot be instantiated. :-S And I have no idea what else to put in my main method to get the code running. Need some help please.
    Thank you.

    Hi paulcw,
    Thank you. I've modified my code but it still doesn't print "roar" or "trumpet". When it returns a Lion or an Elephant, wouldn't the soundOff() method get printed too?
    refactored:
    import java.util.Scanner;
    interface Animal {
        void soundOff();
    class Elephant implements Animal {
        public void soundOff() {
            System.out.println("Trumpet");
    class Lion implements Animal {
        public void soundOff() {
            System.out.println("Roar");
    class TestAnimal {
        public static void main(String[] args) {
            Animal a = get();
        public static Animal get() {
            Scanner myScanner = new Scanner(System.in);
            System.out.println("Meat eater or not?");
            String choice = myScanner.nextLine();
            if (choice.equalsIgnoreCase("meat eater")) {
                return new Lion();
            } else {
                return new Elephant();
    }The soundOff() method should override the soundOff(); method in the interface, right? So I'm thinking, it should print either "roar" or "trumpet" but it doesn't. hmm..

  • Some simple questions regarding the Xcode.

    I have some preliminary questions regarding the Xcode (objective C):
    What is the difference between the programming files followed by .h and .m?
    What does an asterisk * followed by a text mean (i.e. *window;)?
    Is there any source that briefly explains the most common codes with examples (nsstring, nsarray, iboutlet, inaction.....etc)?
    Any other tips will be highly appreciated.
    Many thanks.

    Xcode is the IDE.
    Objective-C is the most common language used within Xcode.
    UIWindow *window .....  the * means return the address of the instance variable 'window'.
    An * is called a pointer to a variable.
    Everything you need is explained here:  https://developer.apple.com

  • Simple question: does the Nvidia proprietary driver support OGL ES 2 ?

    Just the question in the title. Does it support opengl es 2.0 (coming with Kwin 4.7)? Will it (if somebody knows) ?

    OpenGL ES is just a stripped down subset of OpenGL, so I see no reason why it shouldn't be. Please correct me if I'm wrong.

  • Simple loop on the main timeline

    Hello,
    I am very very new to actionscripting, so please bear with me.
    I have a very simple banner that I would like to play 3 times and stop.
    How can I do that in Actionscrip 3.0?
    Thanks
    babs

    You're welcome babs.  For the first part of it, I am doing something to prevent you from having to add a frame that you don't visit again.  I declare the var count, but don't assign it a value unless it has none, which it will not have when the animation first starts. If I had started it out in frame 1 with..
    var count:uint = 0;
    then everytime it looped back to frame 1 it would reset count to 0... which would mean you'd have to loop back to frame 2 instead of frame 1.
    So I have it test if count has no value assigned and assign it a zero in that case only.  That way, when it loops back to frame 1, the value of count is not reset because it has a value by that time.

  • A simple question involving the color of a pixel?

    I'm just wondering, I'm using a BufferedImage to load an image and retrieve the pixels by their location in an image. I looked through the APIs, but I can't seem to figure out how to find the shade of each pixel. I understand that each color has a range within the possibilities of the int returned in the method getRGB(). Does the range denote the shade? Or does the alpha value perhaps? I'm not sure. Any help would be appreciated. Thanks!

    What do you mean by the "shade"? Do you mean hue?
    You might want to look at the HSBtoRGB and RGBtoHSB methods. HSB is a way to describe colors via their hue, saturation, and brightness.
    The alpha value indicates the transparency of the pixel.
    Just out of curiosity, what are you doing?

  • A simple question: Using the user-folders differently

    Hello,
    I'm still getting started with my mac, that's why I have a pretty basic question:
    There are these user-folders on every mac: Documents, Music, Pictures etc.
    But since I sort my photos and videos by date, I often have pictures and videos in one single folder and don't want to seperate them in these user-folders pictures and movies.
    That's why I'd like to create a new folder next to Documents, Music... called "Photo_Video" (or something like that) where I can put all my photos and videos in.
    So, my first (stupid) question: Is it ok to create a new folder in /Users/NAME/_____ ? I'm asking this because the existing folders seem to be kind of special (system folders?). Sorry about this, but I want to get everything right.
    My second question: Is there a downside of NOT using "Movies" and "Pictures" for my media? I've heard somewhere that these folders are in some way protected against external usage.
    Thank you!
    -Lemon17.

    Yes, you can create folders inside your Home. The folders there are not terribly special, but the Finder does treat them as such by putting that badge on the folders.
    If you have multiple users and want to keep the folders private, you'll need to change the user permissions on the folders you create. When you create a new folder, it inherits the permissions of the enclosing folder. Your Home is set to allow other users to see directly inside so that they can see your Public folder and so the Web server can see into the Sites folder.  Because of that, the new folder you create will allow others to see inside of it.
    To change the permissions, select the new Folder and Get Info on it. The info window will open and the permissions are at the bottom. The list of permissions will have you as the owner and you will have read/write access. It will also have a group (I think Staff), and Everyone. Select any Groups (icon with two silhouettes) and delete them (- button). For Everyone, set it to No Access. If you are the only user and you don't share out folders over the network, then it isn't likely necessary to change the permissions.
    I can't think of a downside of using other folders. The potential downside of not using iPhoto is you won't have the media browser to select photos from. But, that's not a really big deal if you can easily identify your photos in the file structure.

  • Re run the main method while error..

    hi all
    i got a class which i use to decode pdf to image
    sometimes error happen(not all the time) ...and i dunno why it happens
    in my desperate condition i'm thinking, what about if error happen then i try to re run the class
    my questions are
    1. is java capable doing this? is it using try catch?
    2. how to re run the class?
    anybody has useful link where i can dig some info of them or sample code, that willing to be post here?
    thx in advance

    public void doSomething(){
      try{
        //something
      }catch(Exception e){
        e.printStackTrace();
        doSomething();
    }You should add a counter and only try a few times, otherwise you might run into an infinite loop with a out of memory exception.

  • A simple question about the Integration process and the PI system

    Hi all
    Out PI system gets more and more slowly .
    in the Monitor I can se that there are many ufinished IP about 100.
    in the Outbound colon there is a Watch icon with the message
    QMessage being sent                     
    and in the graphic of the IP thers is a Send Message Asynchronously  with text in process.
    my question is can those unfineshed IP be guilty in slowing down the system by taking resources, they have been there for quit a long time .
    Thanks

    Hi again
    I have checked sm12 , no locks
    theres is no mapping involved in this step , the IP is trying to send a file to  server.
    I just want to know if  100 unfinished IP slow down the system .
    It does not matter if the file has been sent or not
    is there any way to check those IP if they are taking resources.
    Thanks.

Maybe you are looking for

  • Hi, I have a problem with hyperlinks when using Acrobat 5.0, can anyone help?

    In the past, the hyperlinks have worked OK but I'm wondering if the more recent versions of the Readers will not recognise them. I am making PDFs from Word 2003. The hyperlinks work OK in Word - just not in the PDF. Hope someone can help.

  • I am not sure why, but my mac just keeps on freezing

    Everything was fine, but then my mac ran out of battery and then shut off. When I reconnected it to a charger and started it up, I started seeing this problem. First, the start up took a really long time. Then when I tried to connect to the wifi, i s

  • I am trying to connect my Canon MG5220 and its not connecting to my macbook running lion?

    i am trying to connect my Canon MG5220 and its not connect to my macbook running lion? i updated the drivers to 10.7 lion and i reconnected the printer to my wifi which both devices are connected to. when reinstalling the drivers, it will not connect

  • Photoshop CS2/Elements 4 install problems!

    This has been bugging me since I got both programs; whenever I attempt to install them, when it tries to install "shared items", it says a "required file is missing", and then tells me to restart the installation. I have no idea what this "required"

  • Trying to Publish

    Hello all. I'm trying to publish a file for flash(.swf) and HTML(.html) I'm not sure what going on, but whenever I preview it, it plays fine. once I click the publish button. I see the progress bar for about a second, but there is no published files