Results 1 to 12 of 12
  1. #1
    DewPac is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default executing JAVA from 'system' command

    Hi,

    I'm trying to run a simple java command as mentioned below from 'system' command in a c++ dll loaded on to WMI service

    java -classpath C:\temp helloworld.class

    I have jre 1.6 installed on the machine. This command works fine from my command prompt. the java path is also set in PATH environment variable. But, the above command does not work with 'system' command

    I was told that JAVA path is not set in PATH in non-interactive environment.
    Any pointers please?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DewPac View Post
    Hi,

    I'm trying to run a simple java command as mentioned below from 'system' command in a c++ dll loaded on to WMI service

    java -classpath C:\temp helloworld.class

    I have jre 1.6 installed on the machine. This command works fine from my command prompt. the java path is also set in PATH environment variable. But, the above command does not work with 'system' command

    I was told that JAVA path is not set in PATH in non-interactive environment.
    Any pointers please?
    You don't run a .class file nor do you mention it in a classpath; do this instead:

    java helloworld

    The C/C++ system(char*) function starts another shell with the command given and passes the entire environment of the parent process to the shell process so a path variable is passed as well.

    kind regards,

    Jos

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Wouldn't they need the classpath anyway?
    Unless the execution environment is already in C:\temp, which I would have thought unlikely...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Wouldn't they need the classpath anyway?
    Unless the execution environment is already in C:\temp, which I would have thought unlikely...
    Most likely the -classpath flag is needed but not the way the OP used in the original example because that didn't make any sense at all.

    kind regards,

    Jos

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    I assume helloworld was in C:\temp, which meant that path needed to be included.
    I would expect a straight "java helloworld" to give a class not found unless you were executing the calling code from C:\temp.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    I assume helloworld was in C:\temp, which meant that path needed to be included.
    I would expect a straight "java helloworld" to give a class not found unless you were executing the calling code from C:\temp.
    Darn, I missed a space and added a backslash in my imagination; ignore all my replies in this thread please ...

    kind regards,

    Jos (<--- needs glasses ;-)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    You're imagining back slashes?
    Step away from the IDE and have a lie down!

    ...or a drink...

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    You're imagining back slashes?
    Step away from the IDE and have a lie down!

    ...or a drink...
    I'll go for the second option and have a Grolsch; good tip, a very good one; the best I've read today actually ; thanks for that.

    kindest regards,

    Jos ;-)

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You're imagining back slashes?
    Yup, Jos is a known masochist, always fantasizing about being tortured ;)

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Yup, Jos is a known masochist, always fantasizing about being tortured ;)
    Am not! I only dream about soft and furrry back slashes, not those pointy ones!

    kind regards,

    Jos ;-)

  11. #11
    DewPac is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks everybody for the replies. There is still no luck for me.

    I had actually tried java -classpath C:\temp helloworld. Then I tried again with no classpath, -cp and
    system ("C:\\Windows\\System32\\java -cp C:\temp helloworld"), also
    giving full path where jre was installed, nothing is working. system command returns value '1'.

    The same command seems to work on other setups as well. I'm lost!

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Java Code:
    system ("C:[B]\\[/B]Windows[B]\\[/B]System32[B]\\[/B]java -cp [B]C:\temp[/B] helloworld")
    If you found you needed double slashes in the first part, then do you possibly need them in the classpath bit?

Similar Threads

  1. executing linux command from Java not working.
    By baozhi in forum Advanced Java
    Replies: 2
    Last Post: 03-24-2010, 06:32 AM
  2. problem in executing linux command
    By Basit56 in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 03:53 PM
  3. Executing external command through JSP
    By rudranee in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-21-2009, 09:22 AM
  4. Replies: 0
    Last Post: 06-20-2008, 09:02 AM
  5. Problem during executing Command Prompt
    By keshari in forum Advanced Java
    Replies: 4
    Last Post: 06-05-2008, 04:06 AM

Tags for this Thread

Posting Permissions

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