Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exec method

 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to accomplish by executing the OS "CD" command?
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic