Calling File.delete()

409 views
Skip to first unread message

Raju Bitter

unread,
Feb 20, 2011, 6:27:59 AM2/20/11
to rin...@googlegroups.com
I'm trying to delete a directory/file using the following code:

var File                = java.io.File;
var FileUtils = org.apache.commons.io.FileUtils;
var file = new File("./docs");
print(file.isDirectory());
var success = FileUtils.deleteDirectory(file);

And get the error:
>> FileUtils.deleteDirectory(file);
TypeError: Cannot call property deleteDirectory in object [JavaPackage
org.apache.commons.io.FileUtils]. It is not a function, it is
"object". (<stdin>#65)
at <stdin>:65

What is the problem with this code? I guess I'm missing something.

Raju Bitter

unread,
Feb 20, 2011, 6:28:51 AM2/20/11
to rin...@googlegroups.com
Sorry, the subject should have been "Calling FileUtils.deleteDirectory(file)"

Simon Oberhammer

unread,
Feb 20, 2011, 8:07:27 AM2/20/11
to rin...@googlegroups.com
On Sunday, February 20, 2011 12:27:59 PM UTC+1, Raju Bitter wrote:
I'm trying to delete a directory/file using the following code:

var File                = java.io.File;
var FileUtils = org.apache.commons.io.FileUtils;
var file = new File("./docs");
print(file.isDirectory());
var success = FileUtils.deleteDirectory(file);

And get the error:
>> FileUtils.deleteDirectory(file);
TypeError: Cannot call property deleteDirectory in object [JavaPackage
org.apache.commons.io.FileUtils]. It is not a function, it is
"object". (<stdin>#65)
at <stdin>:65


try with Packages.org.apache.commons.io.FileUtils

`java` is just a shortcut for `Packages.java` and the error message is very misleading, because:

>> Packages.foo.bar.doesNoExist()
TypeError: Cannot call property doesNoExist in object [JavaPackage foo.bar]. It is not a function, it is "object". (<stdin>#24)

  simon

Raju Bitter

unread,
Feb 20, 2011, 6:44:10 PM2/20/11
to rin...@googlegroups.com, Simon Oberhammer
Thanks, Simon. I had put the commons IO jar into ringo.home/lib, but
it doesn't get added to the classpath. Loading the JAR by doing

addToClasspath("./lib/commons-io-2.0.1.jar")

solved the problem.

But there's another problem I ran into earlier. I tried to use
File.delete(), but Rhino chokes on that, since delete is keyword in
Rhino and E4X. Is there a default approach to wrap method names for
Java classes when the method name is a JavaScript keyword?

Try this code to see the error message:
>> var File = Packages.java.io.File;
>> var file = new File("someFile.txt");
>> file.delete()
js: "<stdin>", line 9: missing name after . operator
js: file.delete()
js: ...........^

> --
> You received this message because you are subscribed to the Google Groups
> "RingoJS" group.
> To post to this group, send email to rin...@googlegroups.com.
> To unsubscribe from this group, send email to
> ringojs+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/ringojs?hl=en.
>

Robert Thurnher

unread,
Feb 20, 2011, 7:01:20 PM2/20/11
to rin...@googlegroups.com
On 21 February 2011 00:44, Raju Bitter

<r.bitter.m...@googlemail.com> wrote:
> Thanks, Simon. I had put the commons IO jar into ringo.home/lib, but
> it doesn't get added to the classpath. Loading the JAR by doing
>
> addToClasspath("./lib/commons-io-2.0.1.jar")
>
> solved the problem.
>
> But there's another problem I ran into earlier. I tried to use
> File.delete(), but Rhino chokes on that, since delete is keyword in
> Rhino and E4X. Is there a default approach to wrap method names for
> Java classes when the method name is a JavaScript keyword?
>
> Try this code to see the error message:
>>> var File = Packages.java.io.File;
>>> var file = new File("someFile.txt");
>>> file.delete()
> js: "<stdin>", line 9: missing name after . operator
> js: file.delete()
> js: ...........^

The following (way of accessing JS object properties) should solve this:

`>> file['delete']()`

HTH.

-- Robi


> On Sun, Feb 20, 2011 at 2:07 PM, Simon Oberhammer <si...@nekapuzer.at> wrote:
>> On Sunday, February 20, 2011 12:27:59 PM UTC+1, Raju Bitter wrote:
>>>
>>> I'm trying to delete a directory/file using the following code:
>>>
>>> var File                = java.io.File;
>>> var FileUtils = org.apache.commons.io.FileUtils;
>>> var file = new File("./docs");
>>> print(file.isDirectory());
>>> var success = FileUtils.deleteDirectory(file);
>>>
>>> And get the error:
>>> >> FileUtils.deleteDirectory(file);
>>> TypeError: Cannot call property deleteDirectory in object [JavaPackage
>>> org.apache.commons.io.FileUtils]. It is not a function, it is
>>> "object". (<stdin>#65)
>>> at <stdin>:65
>>
>> try with Packages.org.apache.commons.io.FileUtils
>> `java` is just a shortcut for `Packages.java` and the error message is very
>> misleading, because:
>>>> Packages.foo.bar.doesNoExist()
>> TypeError: Cannot call property doesNoExist in object [JavaPackage foo.bar].
>> It is not a function, it is "object". (<stdin>#24)
>>   simon
>>
>>
>
>

--
http://robert42.com/

Raju Bitter

unread,
Feb 21, 2011, 12:04:44 AM2/21/11
to rin...@googlegroups.com, Robert Thurnher
Right, thanks, that works.

- Raju

Reply all
Reply to author
Forward
0 new messages