aspose file tools*
The moose likes Beginning Java and the fly likes exec method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exec method" Watch "exec method" New topic
Author

exec method

Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
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.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
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.
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
What are you trying to accomplish by executing the OS "CD" command?


Mark Patrick<br />SCJP 1.4
Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
The idea is to use this program to display the path in the screen prermanently for a reports analyzer application called Discoverer.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: exec method