Results 1 to 6 of 6
  1. #1
    skg2910 is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Converting to Object/Classes

    I have the following code i wrote to manipulate terminal commands in linux(CentOS)



    import java.util.Scanner;
    public class JavaExecuteShellCMD{
    public static void main(String[] args) throws Exception{
    String cmd = "pwd"; //Command "pwd"
    String cmd1 = "ls -l";

    Runtime run = Runtime.getRuntime(); //create a runtime

    Process pr = run.exec(cmd);
    pr.waitFor();
    Scanner in = new Scanner(pr.getInputStream()); //take input from terminal
    while(in.hasNext()){
    System.out.println(in.nextLine());
    }
    in.close();

    Process pr1 = run.exec(cmd1);
    pr1.waitFor();
    Scanner in1 = new Scanner(pr1.getInputStream());
    while(in1.hasNext()){
    System.out.println(in1.nextLine());
    }
    in1.close();


    }
    }


    Though I know how to create simple objects and classes, i do not know how to put this in class form.
    Any help would be appreciated.
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting to Object/Classes

    What are you trying to do with this code ultimately? What have you tried to do in your effort to make an OOP-compliant class, and where exactly are you stuck?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting to Object/Classes

    To clarify my questions above, certainly we could tell you to plop all the code above into a class constructor, but to what purpose? And how would this help in any way? Unless we know what you're trying to ultimately do with this code, how you're going to want to use it, our advice will be limited.

    Also, please read my link on how to use code tags so that you can edit your post and allow your code to retain its formatting and be readable.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Converting to Object/Classes

    Read arp cache?

    Same problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    skg2910 is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: Converting to Object/Classes

    what am trying to do is for example a Class vehicle, i want to instantiate 4 vehicles. same here, i want to have the code written in a class so i can instantiate and use it more than once. For example, i want to create an object that will use "pwd" and a second object "ls -l"

    what i managed to do which i guess is quite wrong:

    Java Code:
    public class ShellCMD {
    	private String cmd;
    
    	ShellCMD(String cmd){
    		this.cmd = cmd;
    	}	
    	public void setCmd(String cmd) {
    		this.cmd = cmd;
    	}
    	public String getCmd() {
    		return cmd;
    	}
    }
    Java Code:
    import java.util.*;
    public class ShellExecute {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("1.Current Directory Path\t2.Directory Listing");
    		System.out.println("Make a choice: ");
    		int choice = input.nextInt();
    		
    		switch(choice){
    		case 1:{
    			ShellCMD("pwd");
    			System.out.println("pwd object created");
    			break;
    		}
    		case 2:{
    			ShellCMD("pwd");
    			System.out.println("ls -l object created");
    			break;
    		}
    		}
    	}
    
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Converting to Object/Classes

    Since my earlier response was not visible, I guess this one won't be noticed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Converting primitive int to Intger object?
    By NewbieDan in forum New To Java
    Replies: 7
    Last Post: 02-15-2012, 12:33 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. Converting an array in an object
    By fiefa in forum New To Java
    Replies: 1
    Last Post: 01-31-2011, 04:18 PM
  4. stumpt. converting an object to a long int.
    By root_demon in forum New To Java
    Replies: 7
    Last Post: 10-31-2008, 05:32 AM
  5. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM

Posting Permissions

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