Tech Support banner

Status
Not open for further replies.
1 - 6 of 6 Posts

·
Registered
Joined
·
147 Posts
Discussion Starter #1
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
 

·
Registered
Joined
·
147 Posts
Discussion Starter #2
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;
}
 

·
Registered
Joined
·
26 Posts
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.
 

·
Registered
Joined
·
147 Posts
Discussion Starter #4
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
 

·
Registered
Joined
·
26 Posts
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.
 

·
TSF Team Emeritus
Joined
·
837 Posts
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...
 
1 - 6 of 6 Posts
Status
Not open for further replies.
Top