How to execute the ldapsearch command by java class in linux os?

hi,all
I want to query the users by ldapsearch command. I call the command in my java class. In windows, It work well. But in linux , there haven't any result. My test program is following:
===============
import java.util.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class test{
public static Vector queryAllUser(){
try{
Vector v=new Vector();
String cmdquery="ldapsearch -h localhost -b\"dc=metasphere,dc=com\" \"uid=*\" uid ";
//String cmdquery="ls -la";
Process process=Runtime.getRuntime().exec(cmdquery);
System.out.println("cmdquery:"+cmdquery);
InputStreamReader ir=new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
System.out.println("input="+input);
String line;
while ((line = input.readLine ()) != null){
System.out.println("Line:"+line);
if(line.startsWith("uid")){
String temp=line.substring(line.indexOf(":")+1,line.length());
v.add(temp);
return v;
}catch(Exception e){
e.printStackTrace();
System.out.println("runtime error:"+e.getMessage());
return null;
public static void main(String argv[]){
Vector v=queryAllUser();
for(int i=0;i<v.size();i++){
System.out.println((String)v.get(i));
================
In fact, when I execute the command: ldapsearch -h localhost -b"dc=metasphere,dc=com" "uid=*" uid
in linux prompt, the screen show the results:
version: 1
dn: uid=toppymgt,dc=metasphere,dc=com
uid: toppymgt
dn: uid=qutao,dc=metasphere,dc=com
uid: qutao
dn: uid=zz,dc=metasphere,dc=com
uid: zz
dn: uid=z,dc=metasphere,dc=com
uid: z
dn: uid=admin,dc=metasphere,dc=com
uid: admin
dn: uid=alan,dc=metasphere,dc=com
uid: alan
dn: uid=misssixty,dc=metasphere,dc=com
uid: misssixty
dn: uid=channelv,dc=metasphere,dc=com
uid: channelv
What's reason? Please help me. Thx.

If there have a bugs in Linux OS. Please help me!

Similar Messages

  • How to execute a shell command in java?

    here, my environment is redhat 7, jdk 1.5.
    i don't know how to use the shell command in java.
    i want to use this function:
    #include <stdlib.h>
    int system(const char * string);
    please give me some ideas. and Thank you so much if coming with a little demo.

    i know i should use JNI. because i have to use C lib.
    but i have already use JNI to wrapper the original code the cpp code and java code is :
    //: appendixb:UseObjImpl.cpp
    //# Tested with VC++ & BC++. Include path must
    //# be adjusted to find the JNI headers. See
    //# the makefile for this chapter (in the
    //# downloadable source code) for an example.
    #include <jni.h>
    #include <stdlib.h>
    extern "C" JNIEXPORT void JNICALL
    Java_UseObjects_changeObject(
    JNIEnv* env, jobject, jobject obj) {
    jclass cls = env->GetObjectClass(obj);
    jfieldID fid = env->GetFieldID(
    cls, "aValue", "I");
    jmethodID mid = env->GetMethodID(
    cls, "divByTwo", "()V");
    int value = env->GetIntField(obj, fid);
    printf("Native: %d\n", value);
    env->SetIntField(obj, fid, 6);
    env->CallVoidMethod(obj, mid);
    value = env->GetIntField(obj, fid);
    system("preprocess -path sha.c");
    printf("Native: %d\n", value);
    } ///:~
    //: appendixb:UseObjects.java
    // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
    class MyJavaClass {
    public int aValue;
    public void divByTwo() { aValue /= 2; }
    public class UseObjects {
    private native void
    changeObject(MyJavaClass obj);
    static {
    // System.loadLibrary("UseObjImpl");
    // Linux hack, if you can't get your library
    // path set in your environment:
    System.load(
    "/root/jproj/UseObjImpl.so");
    public static void main(String[] args) {
    UseObjects app = new UseObjects();
    MyJavaClass anObj = new MyJavaClass();
    anObj.aValue = 2;
    app.changeObject(anObj);
    System.out.println("Java: " + anObj.aValue);
    } ///:~
    i modify this two file which is from TIJ-2edition.
    the output is
    Native: 2
    Native: 3
    Java: 3
    but what i want to be executed "preprocess -path sha.c" does not work.
    i have change the command to, such as "df", "ls" etc. but none of them works
    please help me.

  • Executing the top command through Java in linux

    I am trying to execute the top command in Java as
    Runtime.getRuntime().exec("top -n 1 >a.log");
    But the command is not working in linux only through java.When i run the same command through the prompt it is working fine.Also all other commands are working fine.Is there any issue with top in linux?

    flounder wrote:
    Try reading [this article|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]. If it does not answer your problem then try seaching for other similar articles.
    The cited article does provide an answer to the problem since it explains how the shell meta character '>' must be interpretted by a shell. The code as presented does not invoke a shell.

  • How to execute a Unix Command in java

    Hi, Iam trying to execute a unix command on Sun Solaris by passing that command to a java program. How can I achieve this?
    Thanks in advance.

    Have a look at the javadoc around the Runtime.exec() method. If the command is a shell command then you might have to execute a shell as well as the command.
    For example, if you wanted to run a unix command 'ls -l > output.txt' the you might have to pass the following string into the exec() method,
    "/bin/sh ls -l > output.txt'

  • How to execute any cmd command from java application?

    Hi all,
    How to execute or call any command of cmd from java application??
    Is there any method to do so??
    Or, is it possible to do it using Runtime.exec() ??? And if so, how to use it, please explain with ab example...
    I'll highly appreciate....
    Thank you.

    If google would be the best option, then I would not be on Sun's forums and I would not have asked experts like you, sir !! :-)
    Neway, I got the solution from PhHein !!
    Good link indeed..
    Cheers..

  • How to execute  the CONNECT command in sql

    Hi ,
    I am using DB2 database and I need to execute the sql statement
    CONNECT TO Database user username USING password.
    Can anyone tell me how to execute this query in java.
    I tried this by using executeQuery but it was giving a sexception somtehing as follows
    COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0637E QUERY cannot be found.
         at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throwInvalidQueryError(SQLExceptionGenerator.java:577)
         at COM.ibm.db2.jdbc.app.DB2Statement.executeQuery(DB2Statement.java:597)
         at First.detail(First.java:18)
         at First.main(First.java:29)
    can anyone of you help me out in solving this problem

    You need a connection to use JDBC stuff. When you have a connection, you are already logged in. There is no need for a query to connect.

  • How to execute MS DOS command through Java program???

    Dear Sir,
    I want to run a MS-DOS command through my Java program. I have tried "Dir" command but no other command which takes command line args doesn't work at all. Please help.
    import java.io.*;
    class CommandPrompt
         public static void main(String[] args)
              try
                   File file = new File("C:/Temp/Java");
                   String[] cmd = {"command.com","/c","md folder"};
                   String[] envp = {""};
                   Process m;
                   String s = "";
                   m = Runtime.getRuntime().exec(cmd,null,file);
                   BufferedReader buf = new BufferedReader(new InputStreamReader(m.getInputStream()));
                   while ((s = buf.readLine())!=null)
                        System.out.println(s);
              catch (Exception ex)
                   System.out.println("Exception is "+ex);
                   ex.printStackTrace();

    1. This forum is for Swing-related issues only. This question should be posted to the "Java Programming" forum.
    2. Please enclose your sample code in code blocks; it's much easier to read that way. See here for how it's done: http://forum.java.sun.com/faq.jsp#messageformat
    3. Please provide more information, like what error messages you got and what OS you're running. For instance, if you're running WinXP, Win2k or NT4, your command processor should be "cmd.exe", not "command.com". When I made that change, your program worked for me.

  • How to execute dir dos command in Runtime Execution

    Hi All,
    Does anybody know how to execute the dir command in DOS in the runtime execution
    for example,
    when we open the command prompt,
    c:\Documents and Settings\java> e:
    e:\cd java
    e:\dir *.*
    how we can list the directory in runtime execution?

    Process childProcess = Runtime.getRuntime().exec("cmd /C dir *.*");or try with
    ProcessBuilder pb = new ProcessBuilder("cmd /C dir *.*");
    Process childProcess = pb.start();
    NOTE: We can use the ProcessBuilder class from J2SE 5.0+.
    for further reference check the below link
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=3
    and don't forget to go through API documentation for java.lang package
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/package-summary.html
    and if you want to implement it through Java which provides you a list of Files & directories in the present directory here is an example of such code snippet REF:as insisted by my fellow poster.
    File file = new File("<present_directory_path>");
    if(file.isDirectory()){
       String fileNames[] = file.list();
            for(String fileName:fileNames)
               System.out.println(fileName);
      fileNames = null;
            or for detailed info use
             File files[] = file.listFiles();
             for(File fileEntity:files)
                System.out.println("FILENAME:"+fileEntity.getName+",IS FOLDER:"+fileEntity.isFolder()+",ABSOLUTE PATH:"+fileEntity.toString()+"FILE SIZE:"+fileEntity.length());
             files = null;
    }and please do not forget to go through File API Documentation provided as recommended by my fellow poster.
    hope this might help :)
    REGARDS,
    RaHuL

  • Calling the system commands in Java

    Hi All,
    How to call the system commands in java other than using
    exec() function.For eg, in my linux machine i have used p.exec("exit") to exit from a cshell to the prompt but it is not working.Is there any other solution for this one. Pls. do provide a solution for this.It is quite Urgent.
    Thanks,
    m.ananthu

    Why not SEARCH THE F*ING FORUMS!! This has been answered SO MANY TIMES

  • We are trying to implement a process so that any document that needs to be printed through our Java application will be printed as PDF using Adobe Reader. For which, We created and execute the below command line to call Adobe Reader and print the PDF on a

    We are trying to implement a process so that any document that needs to be printed through our Java application will be printed as PDF using Adobe Reader. For which, We created and execute the below command line to call Adobe Reader and print the PDF on a printer."C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /T "\\<Application Server>\Report\<TEST.PDF>" "<Printer Name>". Current Situation: The above command line parameter when executed is working as expected in a User's Workspace. When executed in a command line on the Application Server is working as expected. But, the same is not working while executing it from Deployed environment.Software being used: 1. Adobe 11.0 enterprise version. 2. Webshpere Application Server 8.5.5.2. Please let us know if there is a way to enable trace logs in Adobe Reader to further diagnose this issue.

    This is the Acrobat.com forum.  Your question will have a much better chance being addressed in the Acrobat SDK forum.

  • How to execute a shell command?

    What class and method should I use to set an environment variable on a local system? And how do I execute a shell command in Java?
    Thanks!

    I don't know about setting environment variables from java, but I do know how to execute system commands (and from there you could modify environment variables).
    Use Runtime.getRuntime().exec()
    There are quite a few pitfalls with using this though...
    1. It's platform dependent
    2. Doing this creates a Process. If this process has output, you need to read this, otherwise the output buffer will overflow, and that would be bad... Input is not so bad (either supplying it or not ;)
    3. If you use Win for example, and want to do a "dir", there are complications, as there is no dir.exe file, and hence it can't be executed... Do a search for this topic, there's plenty on how to get around it.
    Anyway, there's plenty more info on Runtime.getRuntime().exec(), have a look around.
    Cheers,
    Radish21

  • Please help How can i run Os commands thru Java programs

    Hey,
    I want to stop and restart the linux server thru java program.Is it possible to run the os commands thru java program.
    I had it thru Runtime.getRuntime().exec("*.exe");
    it only runs the exe files.How can run files other than exe files like .bat,com ans shell commands..Any body knows please help with the code..or mail to this address
    [email protected]
    thankyou,
    regards,
    j.mouli

    What about "start command.com /C execute.bat", or using the overload that takes a String[] as argument?
    What if you use the the full path of execute.bat?
    What error code do you get?
    And what comes th linux, I'm not sure... you'll need a shell interpreter there too, me thinks. (never had to run anything with runtime.exec on linux). Check the man pages if csh, bash, ksh, or what ever shell you like.

  • Can I execute MySql's command from java application?

    Can I execute MySql's command from java application? And how?
    For example :
    load data local infile 'D:\\myData.txt'
    into table myTable
    fields terminated by ';'
    lines terminated by '\n';

    1. get the jdbc driver for mysql from the mysql site at: http://dev.mysql.com/downloads/connector/j/5.0.html
    2. follow the installation instructiions... which you'll also find in your mysql manual...
    Happy travels. Keith.

  • How to execute a SELECT statement  in java??

    Hello !!
    In my java program , i need to delete a record of number X, so
    i accept the number X from the keyboard
    Then before deleting it
    i want the program to show me the name, age of the record which has the number X
    How to do this

    hello kylas
    actually i didnt get why this program example?? wats
    its executing ??? Look at reply #3 in your other thread:
    http://forum.java.sun.com/thread.jspa?threadID=713289&messageID=4126346
    Notice the similarity? You've now asked "How to delete a record in Java" and "how to execute a select statement in java". You may have noticed that, aside from the SQL and the call to execute and executeUpdate (for delete) it's the same code. This is because you don't so much do these things in Java, you do them in SQL. The Java code simply asks the Driver to execute whatever SQL you write. So, I really hope your next post isn't "how do I execute an UPDATE statement in Java".
    Good Luck
    Lee

  • How to get the source code of Java Concurrent Program?

    Hi,
    How to get the source code of Java Concurrent Program?
    Example
    Programe Name:Format Payment Instructions
    Executable:Format Payment Instructions
    Execution File Name:FDExtractAndFormatting
    Execution File Path:oracle.apps.iby.scheduler
    Thanks in advance,
    Senthil

    Go on Unix box at $JAVA_TOP/oracle/apps/iby/scheduler
    You will get class file FDExtractAndFormatting.
    Decompile it to get source code.
    Thanks, Avaneesh

Maybe you are looking for