Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

how to execute commands from java

Thread view: 
peczcyberworld - 30 May 2007 05:33 GMT
hi,
i m trying to execute a CVS command from a java environment. i m
writing this code to create a user. for this i m executing this cvs
command "cmd /c cvs passwd -r <username> -a <new-username>"
after executing this cmd the command prompt will prompt for a pasword
and then after entring the password i have to retype the password for
confirmation. the existing code execcutes the command but i don t know
how to read the prompt for password.
<code>
import java.io.*;
import java.lang.*;

public class first {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("cmd /c cvs passwd -r
gopalakrishnan_k -a ram");
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader OptFromCmd = new BufferedReader(new InputStreamReader(
p.getInputStream()));

BufferedReader fromKeyboard = new BufferedReader(reader);
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
OutputStream stdOut = p.getOutputStream();
String s;
String pswd = "mahesh";
System.out.println("Success");

int i = 0;

while ((s = OptFromCmd.readLine()) != null) {
//s = OptFromCmd.readLine();
i++;
System.out.println(s);

System.out.println(i);

if (i > 1) {
stdOut.write(pswd.getBytes());
stdOut.flush();
}

}

System.out.println(
"Here is the standard error of the command (if any):\n");

while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

/* Your Password Here */
String password="Password Please";
stdOut.write(password.getBytes());
stdOut.flush();
stdOut.write(password.getBytes());
stdOut.flush();
/* Your Password here */

OptFromCmd.close();
stdError.close();
stdOut.close();
} catch (Exception e) {

e.printStackTrace();
}
}
}

</code>

[o/p]

this program gives the output as
adding the user <username>
password:

now i need to enter the password from java envronment and provide it
in the command prompt .
then it prompts for the password again for confirmation so i have to
retype the password again
how to acheive this.
jon - 30 May 2007 18:20 GMT
> hi,
> i m trying to execute a CVS command from a java environment. i m
[quoted text clipped - 82 lines]
> retype the password again
> how to acheive this.

p.getInputStream() will get you the stdIn for the cmd process, in the
same way as p.getOutput stream gets you the output stream.

not sure if that answers your question
btw you may need to thread the input to the inputStream of the process
cus reading process output stream can block.
Arne Vajhøj - 09 Jun 2007 19:03 GMT
> i m trying to execute a CVS command from a java environment. i m
> writing this code to create a user. for this i m executing this cvs
[quoted text clipped - 3 lines]
> confirmation. the existing code execcutes the command but i don t know
> how to read the prompt for password.

Instead off running an EXE why not try with a pure Java implementation
of CVS.

Google finds:

http://www.jcvs.org/
http://javacvs.netbeans.org/library/

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.