Forums Register Login

exec method

+Pie Number of slices to send: Send
Hi,

I want to use exec method and I don't know how to implement it.

I have the following code:

class F
{
public void main(String args[])
{
System.exec("cd");
}
}

What I want to do is to execute cd command from Java enviroment. After compiling this code I obtanin the following error:

Cannot resolve Symbol: method exec (java.lang.String)
Location java.lang.System
System.exec("cd");
^
1 error

I type import java.lang.System and import java.lang.String but the error doesn't dissapear.

I would like what's wrong.

Thanks.
+Pie Number of slices to send: Send
First, there's no such method as System.exec(). The exec() methods are in the Runtime class.

You never need to import java.lang classes. If you get a compile-time error, it's never worth trying that. Note however that, if you use sub-packages of java.lang, such as java.lang.reflect, you would need to import those (but not in your example).

If your example, of running "cd", is what you really intend to do, then it won't work. If it ran at all, which it probably won't, it would only change the working directory of the spawned process, not the Java one. In fact, you can't change the working directory of a Java process (someone correct me if I'm wrong). There is, however, a version of exec() that allows you to specify a working directory for spawned processes.
+Pie Number of slices to send: Send
Peter,

When you say spawned processes what are you refering to? I only want to execute a command like this in C:

system("cd"); //here you can invoke the operating system command cd.

I'm new in Java, but I think there is a command to execute an operating system command.

Thanks.
+Pie Number of slices to send: Send
What are you trying to accomplish by executing the OS "CD" command?
+Pie Number of slices to send: Send
The idea is to use this program to display the path in the screen prermanently for a reports analyzer application called Discoverer.
+Pie Number of slices to send: Send
Ah, well you don't need to spawn a process to do that.

You can get the current directory in Java much more easily. Try System.getProperty("user.dir").

This will be much faster and is also platform-independent.

If the value of that isn't an absolute path, and you want an absolute path, you may want to construct a java.io.File from the property value, then use getAbsolutePath() or getCanonicalPath() on it.
+Pie Number of slices to send: Send
Peter,

I typed what you posted and it's ok but it isn't what I want. What I want is to get the real path, that is, the path got from the operating system directly because always is different.

I don't know if you know C (I so) and with this language I can invoke an operating system command using system("OS command"). Is it possible to do it in Java? I think so.

Thanks.
+Pie Number of slices to send: Send
 


I don't know if you know C (I so) and with this language I can invoke an operating system command using system("OS command"). Is it possible to do it in Java? I think so.



Running the "cd" command won't work in C either. All the system() command does is start a command shell, to change the current working directory, which will be immediately be lost when the command shell exits.

However, you can do something similar with the Runtime class, you just have one level of indirection. You need to exec the shell which can exec the command.

Henry
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1380 times.
Similar Threads
Groovy stub compilation through ant script is giving err
package import doubt
separate JVM's
System.setProperty method not recognized?
Printing sequence - Threads
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:55:22.