Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Shoss

Thread: Java from console with parameters... with inetrnal parameters

  1. #1
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Java from console with parameters... with inetrnal parameters

    Hi,

    I am a beginner in Java, and I have a problem.

    I have a java program, which is running in console.

    When I run it as "java -cp ...., program runs and waits for my internal command with parameters like this command_name -param_name param.

    How to run this program from console with initial internal command with internal parameters, something like this way :
    java -cp ..... command_name -param_name param ??
    As I showed it doesnt work.

    Thanks in advance

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Java from console with parameters... with inetrnal parameters

    Not sure what you're asking. -cp sets the class path if you are using external libraries. If you just want to run your java program, do something like this

    java MyProgram

    ... Assuming you have compiled it and are in the same directory as the resulting class file.

  3. #3
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    I'm also not sure what you're asking, but i think its how to add parameters to the command line?

    If that is the case, your main function is where you want to look. If i have this code -
    Java Code:
    public static void main(String[] args) {
      ...
    }
    args contains all of the parameters passed after the file name, separated by spaces.

    So if my class is called HelloWorld and i run "java HelloWorld this is a test" then the values of args would be -
    Java Code:
    args[0] = "this"
    args[1] = "is"
    args[2] = "a"
    args[3] = "test"

  4. #4
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    Thanks, but I am not asking about it. I want to run "java MyProg internal_prog_command -internal_param_nam param" from the console.

    edit:

    Yes I know, but inside my program I have -name_of_parameter and value_of_parameter...

    Sorry, but I am not native english.

    Inside my program i put commands the way like this.... command -option parameter...
    How to put it from console when starting a program ??
    Last edited by metoda; 01-24-2012 at 07:24 PM.

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    The only way that i know of to interact with your program from the console is how i have shown above. Passing in strings in the args parameter. You can then use their values to run your commands? Maybe it would help if you posted some of your code to show us exactly what you're doing with these commands?

  6. #6
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    When I start the program, it waits for my command. I put this: joinN -port 8080 -bootstrap 192.168.1.101:4242... and it works
    When I run this externally from console java -cp .;build\classes;config;lib\log4j.jar de.uniba.wiai.lspi.chord.console.Main joinN -port 8080 -bootstrap 192.168.1.101:4242 it doesn't work. It only gets command_name "joinN" but without -options ... Why ??

  7. #7
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    Is this code you're writing or some other program you're trying to run?

    If you want all of that to go in as one string, you'd have to enclose them in quotation marks -
    java -cp .;build\classes;config;lib\log4j.jar de.uniba.wiai.lspi.chord.console.Main "joinN -port 8080 -bootstrap 192.168.1.101:4242"
    metoda likes this.

  8. #8
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    This is an open source program OpenChord, which implements Chord algorithm in DHT overlay network.
    It uses sockets. I need to open it about twenty times in separate consoles.

    Whent I open first instance manually and put internal command "joinN" to prepare the network, it works.
    When I open it form console with this internal command "java -cp ...classes MyProgram joinN" it also works.

    When I open second instance and put internal command "joinN -port 8080 -bootstrap 192.168.0.1:4242" it aso works fine.
    But when I open it from console with internal command "java -cp ...classes... MyProgram joinN -port 8080 -bootstrap 192.168.0.1:4242", it does not work, it behaves as only parameter "joinN" is seen.

    edit:
    Ok, Your sollution with " seems to be correct. Thanks a lot.
    Last edited by metoda; 01-24-2012 at 08:24 PM.

  9. #9
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    Ok, I have another question, as is works fine.

    Now, I am running this application from console with internal command with -options, but i want also include next command within this.
    Between this two internal commands the press Enter button is needed. Do You have any sugestion ??

  10. #10
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    These questions would be better in a forum for the OpenChord application as i have no idea how they programmed it to work.

    However, just guessing, you can try passing two commands to the class separated by a space.

    java -cp .;build\classes;config;lib\log4j.jar de.uniba.wiai.lspi.chord.console.Main "joinN -port 8080 -bootstrap 192.168.1.101:4242" "second command"

  11. #11
    metoda is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Re: Java from console with parameters... with inetrnal parameters

    Guessed correct ! Thanks again ;)
    OpenChord mailing list is not up to date... Probably the project is down, so I need to ask here.

Similar Threads

  1. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  2. Replies: 2
    Last Post: 10-24-2010, 04:34 PM
  3. 2 import.java.io.*; and assigning parameters
    By Shaggx in forum New To Java
    Replies: 1
    Last Post: 02-02-2010, 08:00 PM
  4. Netware: JAVA.NLM Version 1.31h Parameters
    By Larry Mateo in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 11:34 PM
  5. Help with parameters values in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •