Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 08-24-2005, 04:33 PM   #1 (permalink)
Registered User
 
C0B01's Avatar
 
Join Date: Jan 2005
Posts: 147
OS: XP, Debian, FREEBSD & DSL


Need help with Java System inputs..

I want to convert the following C code to java

#include<stdio.h>
...
char command[6] = {'d', 'i', 'r', ' ', '-', 's'};
system(command);
...

I've tried using System.in(command); in java, but that wont compile... any suggestions?



thanks,

C0B01
__________________
B.Sc. (hons) Computer Science
C0B01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 08-25-2005, 04:08 AM   #2 (permalink)
Registered User
 
C0B01's Avatar
 
Join Date: Jan 2005
Posts: 147
OS: XP, Debian, FREEBSD & DSL


Here is the part of the java code as stated above.. it doesnt work.

/**
*
* Custom menu item that shuts the computer down. 'Kill Item'
*
*/


public JMenuItem createFileKillItem()
{

JMenuItem item = new JMenuItem("Kill");
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = "Shutdown.exe -r -t 00";
System(command);
//System.in(command); ??!
}
}
ActionListener listener = new MenuItemListener();
item.addActionListener(listener);
return item;
}
__________________
B.Sc. (hons) Computer Science
C0B01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-27-2005, 05:18 AM   #3 (permalink)
Registered User
 
Join Date: Jul 2005
Posts: 24
OS: Windows XP


From what I understand you are trying to invoke a command from within your java program to do something and then return to your java program again. If so:

*snip*
1: String command = "Shutdown.exe -r -t 00";
2: System(command);
3: //System.in(command); ??!
*snip*
I can't seem to understand what you are doing in the second line. Unless you wrote a method called System that is static, then I don't think the compiler will let you do what you've entered in line two above. From what I gather you are trying to call a System method, b/c there was a method in C that was called system that performed similar operations; this is very rarely the case. C standard functions don't map exactly to java methods.
Also AFAIK the System class doesn't have a constructor, and that is not the method to initialiaze a new occurence of an Object. All of the methods in the System class I have used have been static, so an instance of System object isn't even required. Maybe some comments in your code will give a better picture of what you intend.

Take a look at Runtime class in the APIs; java.lang.Runtime. I will let you figure out the finer details, but here are some hints;
1. You need a Runtime object, so found out how you can create/get an instance of the Object.
2. What method you have to call on the Runtime object so that it executes your command.
3. (optional) how to get input, output from the execute program invoked by your command and the exit code (just in case you need to see whether it terminated gracefully or whether there was an error).

Another hint: the system might not be able to find the command you are executing. You can usually find out by going to run and entering the command.

Hope that helps. Post if you have any further problems or if you can't decipher my cryptic explanation.
DJ_Dance is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-28-2005, 04:17 PM   #4 (permalink)
Registered User
 
C0B01's Avatar
 
Join Date: Jan 2005
Posts: 147
OS: XP, Debian, FREEBSD & DSL


Thank you for your reply. Basically I've written a simple C program before that once run either shuts the computer down or reboots (depending on what the user chooses). What I need to find out is how to issue a command to the command prompt through a method in a java program. In the C program I simply set up an array of chars that held the string that if typed into (sent to) the command prompt would have the same effect as running a batch script with the content of "shutdown.exe -s -t 00".

Thanks again
__________________
B.Sc. (hons) Computer Science
C0B01 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-03-2005, 01:55 AM   #5 (permalink)
Registered User
 
Join Date: Jul 2005
Posts: 24
OS: Windows XP


Try this:

Code:
import java.io.IOException;

public class Tester
{
	public static void main(String[] args)
	{
		// Get a new runtime object.
		Runtime prog_runtime = Runtime.getRuntime();

		try
		{
			// Execute the external program. Note that it
			// returns the process which has been spawned
			// as a result of executing the command.
			Process exec_proc = prog_runtime.exec("notepad.exe");

			// Wait for the previous process to
			// exit before continuing.
			exec_proc.waitFor();

			// Get the exit code. Note: that the exit
			// code of the program might be different 
			// for when it's successful, but generally its
			// zero.
			if (exec_proc.exitValue() != 0)
			{
				System.out.println("Command unsuccessful");

				System.exit(-1);
			}
			
			// You shouldn't see this message until the 
			// notepad window is close.
			System.out.println("Process done!!");

		}
		// Handle these exceptions whichever way you like,
		// I am just printing the stack of calls made
		// when the exception was thrown.
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
}
Replace notepad.exe with the program you want to execute.
DJ_Dance is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-03-2005, 07:48 AM   #6 (permalink)
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 840
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
LOL, I Love That C Program, You know in the GNU Gcc headers, under /usr/include/sys/reboot.h you can actually force the computer to do a hard shutdown or cold reboot, just with the call of a command...
__________________
Suicide Command in Linux : rm -rf / ;)
AIM:TheLoneWolf071@aim.com--If You Need Help, Don't Hesitate...
LoneWolf071 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 07:56 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85