Getting Java Interface.class from Lucee

115 views
Skip to first unread message

Jamie Jackson

unread,
Aug 20, 2015, 4:15:09 PM8/20/15
to Lucee
How do i get BasicFileAttributes.class, if doing this from CFML (http://stackoverflow.com/a/2724009/)?

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
BTW, this:
basicFileAttributes = createObject("java", "java.nio.file.attribute.BasicFileAttributes");
writeDump(basicFileAttributes.class);
...throws this (on the dump line):
No matching Constructor for java.nio.file.attribute.BasicFileAttributes() found
Thanks,
Jamie

Jamie Jackson

unread,
Aug 20, 2015, 4:21:36 PM8/20/15
to Lucee
I forgot to mention that my first guess:

basicFileAttributes = createObject("java", "java.nio.file.attribute.BasicFileAttributes").getClass();

...failed with:

No matching Method for getClass() found for java.nio.file.attribute.BasicFileAttributes

Robert Munn

unread,
Aug 21, 2015, 3:30:17 AM8/21/15
to lu...@googlegroups.com
CFML and Java do not play together well with what you are trying to do. Here is a way to read creationTime using java.nio.file.Files:

<cfscript>
paths = createObject("java", "java.nio.file.Paths" );
path = paths.get( "/Users/rmunn/lucee", [] );
//options = createObject("java", "java.nio.file.LinkOption" );
files = createObject("java", "java.nio.file.Files" ).readAttributes( path, "*", [] );
</cfscript>

<cfdump var='#files.creationTime.toString()#'>

You can use [ options.NOFOLLOW_LINKS ] as the third argument of the last command if you want to ignore links…


Robert


-- 
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
--- 
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/c7b902f8-fabe-4ae1-9de6-27ea286cf011%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Munn

unread,
Aug 21, 2015, 3:32:24 AM8/21/15
to lu...@googlegroups.com
and you could change the ‘files’ variable name to ‘attrs’ for clarity...

Adam Cameron

unread,
Aug 21, 2015, 4:11:59 AM8/21/15
to Lucee


On Friday, 21 August 2015 08:30:17 UTC+1, Robert Munn wrote:
CFML and Java do not play together well with what you are trying to do.

I think it's more that Lucee and Java don't play nicely here.

This is a perfectly working version of the Java code in that StackOverflow link, in CFML:

dir = expandPath("./");



paths
= createObject("java", "java.nio.file.Paths");

p
= paths.get(dir, []);


files
= createObject("java", "java.nio.file.Files");


linkOption
= createObject("java", "java.nio.file.LinkOption");
linkOptions
= linkOption.values();


basicFileAttributeView
= createObject("java", "java.nio.file.attribute.BasicFileAttributeView");
basicFileAttributeViewClass
=basicFileAttributeView.getClass();


view
= files.getFileAttributeView(p, basicFileAttributeViewClass, linkOptions).readAttributes();


writeOutput
("#view.creationTime()# is the same as #view.lastModifiedTime()#");

I hasten to add my Java is fairly pedestrian, and this is just the result of taking the code from StackOverflow and doing all the extra stuff CFML needs to do the same thing. It might not be the best way to go about things.


On ColdFusion this outputs:

2015-08-21T07:52:50.951374Z is the same as 2015-08-21T07:53:03.644183Z

(that's obviously nonsense, but it's what the code is supposed to do)

On Lucee it errors cos one cannot call getClass() as indicated earlier.

:-/


Simon Hooker

unread,
Aug 21, 2015, 5:54:12 AM8/21/15
to Lucee
Micha has put up a fix for the issue raised relating to this so hopefully that'll help sort this out!  https://luceeserver.atlassian.net/browse/LDEV-502

Robert Munn

unread,
Aug 21, 2015, 4:00:02 PM8/21/15
to lu...@googlegroups.com

The reason to use/return the BasicFileAttributeView is if you want to use the setTimes() method to change the file time attributes. Otherwise,using the Files.readAttributes() method I used returns a read-only Map of the same file attributes available in the View class. 

So if all we need is read-only access to the attributes, we could condense your code into:

paths = createObject("java", "java.nio.file.Paths" );
path = paths.get( expandPath("./"), [] );
options = createObject("java", "java.nio.file.LinkOption" );
attrs = createObject("java", "java.nio.file.Files" ).readAttributes( path, "*", options.values() );

writeOutput("#attrs.creationTime.toString()# is the same as #attrs.lastModifiedTime.toString()#");



On Aug 21, 2015, at 1:11 AM, Adam Cameron <camero...@gmail.com> wrote:

I hasten to add my Java is fairly pedestrian, and this is just the result of taking the code from StackOverflow and doing all the extra stuff CFML needs to do the same thing. It might not be the best way to go about things.


On ColdFusion this outputs:

2015-08-21T07:52:50.951374Z is the same as 2015-08-21T07:53:03.644183Z

(that's obviously nonsense, but it's what the code is supposed to do)

On Lucee it errors cos one cannot call getClass() as indicated earlier.

:-/



--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

Adam Cameron

unread,
Aug 21, 2015, 5:27:39 PM8/21/15
to Lucee


On Friday, 21 August 2015 21:00:02 UTC+1, Robert Munn wrote:

So if all we need is read-only access to the attributes, we could condense your code into:


Cheers Robert, that's not really the (/my) point here though. The aim was to have working CFML code that demonstrated the incompat between Lucee and ColdFusion. TBH, I didn't even look @ what the code did, I just translated the example Java code into CFML, and checked that it worked (ie: ran, doing whateverTF it was doing... something to do with files... I seriously didn't bother to read it, which is quite odd now that I think about it) on ColdFusion, and did not on Lucee.

It's always good to be shown "better code" though I guess (I didn't look @ your code either though. Someone else no-doubt will!)
Reply all
Reply to author
Forward
0 new messages