الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

linux by java

مغلق
بدأه الساهر2004 في 20 سبتمبر 2006 · 4 رد · 506 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلام عيكم ورحمة الله وبركاتة:

انا عندي مشروع تخرج في الجامعه وهو برمجة اوامر shell arabic under linux المهم

نا بجيد لغة c,java

انا ارغب في برمجة بالغة الجاف

انا بعرف ان الجاف بتنصب علي اي نظام تشغيل

كما تعرفون ان اوامر الشيل كلها مكتوبة بالغة ال c

هل يعرف احد موضوع او ان سبق برمجة اوامر shell بلغة الجاف ان يدلني على الطريق

وجزاه الله خيرا

#2

اذا كنت تنوي استدعاء الاوامر فقط استخدم الامر

 Runtime.getRuntime().exec("/bin/ls");

سيتم تنفيذ الامر ls الموجود في المجلد bin

منقطع من حين لاخر

#3

انا ارغب ان ابرمج اوامر shell بلغة الجاف

هل عندك معلومات حول هذا الموضوع يا ليت تساعدني

وشكرا

#4

يا جماعة انا محتاج رد على هذا الموضوع

بجد عندي مشروع هالترم

وشكرا

#5

السلام عليكم

اولا ل عام وانتم بألف خير

أخي الكريم ابوالفؤاد على ما اعتقد انه يريد يعمل برنامج يعمل عمل اوامر ال shell

صراحة لا أعلم ما هي اوامر ال shell ولكن هذا برنامج بسيط تستطيع من خلاله تنفيذ اوامر

dir cd call exit

طبعا اضف اوامر الshell اللي بتحكي عنها

البرنامع معمول بهذه الطريق حتى تستطيع اضافة ميثود جديدة بكل سهوله

مثلا اذا أردت أضافة الأمر copy

فقط اضف المثود التالية

public static MethodResult copy(String[] args)throws Exception

جميع احرف المثود صغيرة

المثود يجب ان تكون في كلاس اول حرف من اسم الميثود واسم اكلاس يجب ان يكود حرف كبير class C

وهكذا....

اسم الملف shell.java

import java.lang.reflect.*;
import java.io.*;
class MethodResult
{   private String _Message;
	private int _Result;
	MethodResult(String Message,int Result)
	{_Message=Message;_Result=Result;}
	public String getMessage(){return _Message;}
	public int getResult(){return _Result;}
	public void printMessage(){System.out.println(_Message);}
}
class MethodInfo
{   public final String MethodName,ClassName;
	public String[] Args;
	MethodInfo(String cmdLine)
	{   cmdLine=cmdLine.trim();
		int spc_index=cmdLine.indexOf(" ");
		if(spc_index==-1)
			MethodName=cmdLine.toLowerCase();
		else
		{
			MethodName=cmdLine.substring(0,spc_index).toLowerCase();
			Args=cmdLine.substring(spc_index+1).split(" ");
		}
		 String pkg="";
		 if(shell.class.getPackage()!=null)
			{pkg=shell.class.getPackage().getName();
			 if(pkg==null)pkg="";
			 if(pkg.length()>0)pkg+=".";}
		ClassName=pkg+MethodName.substring(0,1).toUpperCase();
	}
}
public class shell {

	public static String CurDir="";
	public static BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

	public static String readLine()throws Exception
		{return stdin.readLine();}
	public static String CurDir()
		{return System.getProperty("user.dir");}
	public static void main(String[] args)throws Exception
	{   String MethodName="";
		String[] Params;
		while (true)
		{System.out.print(CurDir()+">");
		 String cmdLine=readLine().trim();
		 if(cmdLine.length()>0)
			{
			 MethodInfo method=new MethodInfo(cmdLine);
			 MethodName=method.MethodName;
			 //System.out.println(MethodName);
			 try
			 {
				 Method m=Class.forName(method.ClassName).getMethod(MethodName,String[].class);
				 Object res=m.invoke(null,(Object)method.Args);
				 if(res!=null)
					 ((MethodResult)res).printMessage();
			  }
			 catch(NoSuchMethodException e)
			 {System.out.println("Bad Command");}
			 catch(ClassNotFoundException e)
			 {System.out.println("Bad Command");}
			 catch(Exception e)
			 {e.printStackTrace();}
		   }
		// System.exit(0);
		}
	}

}

class A
{

}
class B
{

}
class C
{
	public static MethodResult call(String[] args)throws Exception
	{   boolean missingArgs=true;
		if(args!=null)
		{ missingArgs=false;
		  try{
			Runtime.getRuntime().exec(Function.join(args," "));}
		  catch(Exception e)
		  {return new MethodResult(e.getMessage(),-2);}
		}
		if(missingArgs)
		{
		   return new MethodResult("missing arguments",-1); 
		}
		return null;
	}
	/**
	 *args - ..			 go up one level
	 *	 - \			  go to the root
	 *	 -  folder name   go to this folder
	 *	 -  full path	 go to this path
	 */
	public static MethodResult cd(String[] args)throws Exception
	{
		if(args!=null)
		{   String dir="";
			if(args[0].trim().equals(".."))
			{
			 dir=shell.CurDir();
			 dir=dir.substring(0,dir.lastIndexOf("\\"));
			}
			else if(args[0].trim().startsWith("\\"))
			{
			  dir=shell.CurDir();  
			  dir=dir.substring(0,1)+":\\";
			}
			else if(args[0].indexOf(":\\")==-1)
			   dir=shell.CurDir()+"\\"+Function.join(args," ");
			else
			  dir=Function.join(args," ");

			if(Function.isValedDir(dir))
			   System.setProperty("user.dir",dir);
			else
			   return new MethodResult("cannot find the path specified",-1);
		}
		return null;
	}
}
class D
{   /**
	 *args - null
	 *	 - /p show 10 results each page 
	 */
	public static MethodResult dir(String[] args)throws Exception
	{   String path=System.getProperty("user.dir");
		int resultNum=0;
		if(args!=null)
		   {
			if(args.length>0)
			{if(args[0].toLowerCase().equals("/p"))
			   {resultNum=10;}
			}
		   }
		long totalFileSize=0;
		int fileCount=0,folderCount=0;
		File file = new File(path);
		File[] oDir=file.listFiles();
		String p1="",p2="",p3="";
		for(int i=0;i<oDir.length;i++)
			{  p1=Function.Space(12);
			   p3="   "+oDir.getName();
			   long length= oDir.length();
			   totalFileSize+=length;
			   if(oDir.isDirectory())
				  {folderCount++;
				   p1=Function.addright("	<DIR>",12);
				   p2=Function.Space(20);
				  }
			   else
			   {fileCount++;
				 p2=Function.addleft(""+oDir.length(),20);
			   }
			   System.out.println(p1+p2+p3); 
			   if( resultNum>0 && ((i+1) % resultNum) ==0)
				{System.out.print("Press Enter to show next page or type c and enter to stop");
				 if(shell.readLine().toLowerCase().equals("c")) break;
			   }
			} 

		System.out.println(Function.Space(4)+Function.addleft(""+fileCount,6)  +" File<s>   "+totalFileSize +" Bytes");
		System.out.println(Function.Space(4)+Function.addleft(""+folderCount,6)+" Folder<s> ");
		//if(args.length>0)System.out.println(args[0]);

		return null;
	}
}
class E 
{
	 public static MethodResult exit(String[] args)throws Exception
	 {
		 System.exit(0);
		 return null;
	 }
}
/*
  add more classes
  class F
  class G
  class ...
		......
 */
class Function
{
  public static String Space(int count){return Strings(count ," ");}
  public static String Strings(int count,String lpchr){
	  String retValue=lpchr;
	  for(int i=1;i<count;i++)retValue=retValue.concat(lpchr);
	  return retValue;}
  public static String addright(String str,int LineLen)
   {  if(str.length()>LineLen)return str;
	   return str+Space(LineLen-str.length());}
  public static String addleft(String str,int LineLen)
   {  if(str.length()>LineLen)return str;
	   return Space(LineLen-str.length())+str;} 
  public static boolean isValedDir(String dir)  
  {try
	  {File Dir = new File(dir);
	   return Dir.isDirectory();
	  }catch (Exception e){return false;
  }
}
 public static String join(String SourceArray[],String joinwith)
  {
   int i;
   String retValue="";
   for(i=0;i<SourceArray.length;i++)
	   {
	   if(i==0){retValue=SourceArray[0].toString();}
		else{retValue=retValue.concat(joinwith.concat(SourceArray.toString()));
				/*retValue=retValue.concat(joinwith+SourceArray.toString());*/}			
	   }
   return retValue;	 
  }  
}

تم تعديل هذه المشاركة بواسطة Mo7eb_Alrasool في 23 سبتمبر 2006 في 06:29

سبحانك اللهم وبحمدك أشهد ان لا اله الا أنت

أستغفرك وأتوب اليك

الهم صلي وسلم وبارك على سيدنا وحبيبنا محمد

هذا الموضوع مغلق.

مواضيع مشابهة