Jump to content


Executing Command Line Code From Within Java


  • Please log in to reply
1 reply to this topic

#1 DemiReticent

DemiReticent

    Member

  • Members
  • PipPip
  • 26 posts
  • Gender:Male
  • Location:Texas

Posted 20 June 2008 - 04:26 PM

For anyone familiar with command line, a group of students at my school are trying to write a Java program that can access the command-line accessible program "set," specifically "set /a" for the sake of entering a simple mathematical expression and getting an answer without having to re-write that program to parse input in the form of a mathematical expression.

Essentially, given an input such as:
3 + 4

Utilize "set /a [expression]" syntax from the command line to yield the answer 7 and output to the Java console.

I believe that there is a way to do this with C++, but is this even possible within the Java Virtual Machine?

#2 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 20 June 2008 - 04:42 PM

Look at the exec method of the Runtime class: http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html. This really isn't a good solution though, instead the students should implement a parser themselves or look for third-party libraries that will do it for them. Interfacing with the operating system in this manner kills one of the big benefits of a language such as Java, which is the ability to work cross-platform.