I notice that the File class has a property called folderType, but I cannot see anyway to get this String at run-time. Is there a way? Edit: perhaps it's not part of File.
When I rest my mouse over file in the fileIO.open
The object you have there is a Win32ShellFolder2, which is a subclass of ShellFolder, which is a subclass of java.io.File. ShellFolder defines a getter named getFolderType() which returns the folder type as a string.
So you could get the contents of the field like this:
file = fileChooser.getSelectedFile();
if (file instanceof ShellFolder) {
ShellFolder sf = (ShellFolder)file;
String folderType = sf.getFolderType();
ShellFolder and Win32ShellFolder2 are in the package sun.awt.shell. This package isn't part of the standard Java API, so it could change from one JVM to another or from version of the JVM to another. See With what should I replace sun.awt.shell.ShellFolder so I don't get compile warnings?.
There are many ways one of them is this MimetypesFileTypeMap().getContentType(file)
and i think fileio is object of class Win32ShellFolder2,this has a public function getFolderType() as mentioned here
so you can use it like this i feel
fileIO.open(file).getFolderType()
Related
I'm using ecj for Genetic Programming and I have it built so it takes the best fit individual program after a run, and creates a java class with a function from the lisp code that is created.
I then have my program compile the java file. Is there anyway I can run the newly compiled class file in the same run?
I want to be able to:
RunMain --> Create Java --> Compile Class --> Call function in newly created class --> EndMainRun
So far, I'm having trouble calling the method in the newly created class.
Every time I create the new java file and compile, it rewrites the old one. But whenever that class is called later, it's running the old function pre-overwrite. Any tips would be much appreciated!
Edit: Here's some very basic pseudo code to show what I've got so far, a lot of it is abstracted. Assume that there is already a MathFunction.class file before I run this.
PseudoCode
Main(){
runGeneticProgrammingAlgorithm();
generateJavaFileFromBestFitIndividual(name = MathFunction.java) //Replaces old MathFunction.java
compile(MathFunction.java) //using JavaCompilerApi, replaces old MathFunction.class
double value = MathFunction.calculate(25);
The old function returned -1 for value, the new function should return 5, but it's still returning -1. Even if I put this all in a loop, it'll keep spitting out -1, -1, -1....
Edit 2:
I'm still having it return the same value despite the function being completely different. Here is the code:
URL[] urls = null;
File dir = new File("src" + java.io.File.separator + "ec");
URL url = dir.toURI().toURL();
urls = new URL[] { url };
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("ec.MathSolution");
MathSolution mathFunction = (MathSolution) cls.newInstance();
System.out.println(mathFunction.calculate(123.5));
Edit 3:
Found an amazing source online here: http://www.toptal.com/java/java-wizardry-101-a-guide-to-java-class-reloading
Achieving what you want to do is non-trivial but easily possible with some classloader magic.
The fact that you say 'it's running the old function pre-overwrite'... indicates that you are creating an instance of this class from the same classloader that you got it from the first time around.
I suggest reading up on ClassLoaders.
At a high level your algorithm should go:
Create java
Compile Class
Create a ClassLoader that is a child of your current
classloader.
Load the class compiled at step 2 using this classloader
Instantiate the class or otherwise use it.
Now you see there is a new revision... start from 1
I will try to edit this answer with some code samples.
What I need: I need a method that, given a specific directory name on my app, will return his content.
/root
/folder1
/folder11
/folder12
/folder2
/folder21
/file21.txt
/folder3
/file31.txt
/file32.txt
For example, if I call myMethod("/root"); it will return to me a certain object list containing {'folder1', 'folder2', 'folder3'}. Calling myMethod("/root/folder2"); will return {'folder21', 'file21.txt'}
Question: how can I implement this kind of function using Spring?
You can use java.io.File if it is a directory you list childs with docx.list() and then recursively you can do that for all childs that are directories
I'm attempting to provide a StreamingResponse for files stored under Lifts resources/toserve directory, in order to authorize access for different users.
I can access an image for example with:
localhost:8080/classpath/images/test.jpg
But when I try and actually read the file using scala I keep getting file not found exceptions:
val file = new java.io.FileInputStream("/classpath/images/test.jpg")
Is there a specific method to reading files located on classpath?
Thanks in advance, much appreciated :)
To read resources from the toserve-directory you need to do a call like
LiftRules.getResource("/toserve/images/test.jpg")
If you try to use 'classpath' instead of 'toserve' you will receive an empty box.
By default, Lift uses two different path-prefixes to locate resources either programmatically within the server or through a link-element from HTML. For the former, you will use the 'toserve'-prefix, for the latter the 'classpath'-prefix.
This behavior is specified in the objects net.liftweb.http.LiftRules and net.liftweb.http.ResourceServer. In particular, you can there specify (i.e. replace) the path to the resources. The relevant code is:
/** (from net.liftweb.http.ResourceServer)
* The base package for serving resources. This way, resource names can't be spoofed
*/
var baseResourceLocation = "toserve"
You might also want to look at the following method in LiftRules, which allows you to redefine the name used to serve resources through the server:
/** (from net.liftweb.http.LiftRules)
* The path to handle served resources
*/
#volatile var resourceServerPath = "classpath"
If you wish to use the same prefix to refer both to resources you can use either (or both) of these settings to achieve your purpose.
Have you tried:
LiftRules.getResource("/classpath/images/test.jpg")
That should return a java.net.URL to the item you are looking for.
This may also have more information on what you are looking to do: http://exploring.liftweb.net/master/index-9.html#lst:streaming-download
How do I find out why java.io.File.mkdir() returns false.
I can create the directory manually.
UPDATE:
My code looks like this:
String directoryName = "C:/some/path/";
File directory= new File(directoryName );
if (!directory.exists() && !directory.mkdir()) {
throw new RuntimeException("Failed to create directory: " + directoryName);
}
You will need to use mkdirs() if the parent folder (some in your example) doesn't already exist.
The answer is simple, you're trying to create nested folders (a folder inside a folder). For nested folders use File.mkdirs(). That works, (tested).
I don't think you can, at least not from Java. Being that the OS makes that determination, Java is simply delegating to it and returning the result.
Have you tried ensuring that your File object is pointing where you think it is?
Update: if C:/some does not exist, it must first be created before you can attempt to create C:/some/path. Or use mkdirs() as was pointed out.
If you use something like process monitor for windows you can view the OS level attempt to create the directory.
That may give you the info you need.
You'll probably need to make use of the filters in process monitor because there's usually a lot of disk activity going on :)
Using cygwin?
mkdir may return false, but go on to create the folder anyway. The false seems only to indicate that the folder does not exist, yet.
You may have to try directory.exists() after the mkdir() call (or even mkdirs())
In this ParserFactory.java
String className = System.getProperty("org.w3c.css.sac.parser");
if (className == null) {
throw new NullPointerException("No value for sac.parser property"); //line 35
} else {
return (Parser)(Class.forName(className).newInstance());
}
When I run this DemoSAC.java file as Java Application in Eclipse, I got
Exception in thread "main" java.lang.NullPointerException: No value for sac.parser property
at org.w3c.css.sac.helpers.ParserFactory.makeParser(ParserFactory.java:35)
What exactly is the "org.w3c.css.sac.parser" property? How do I set it under Windows? What should I set it to?
Thanks!
If you don't want to modify code, you can also set a Java property in Eclipse's Java launch configuration dialog. You specify a "VM option" in the "Arguments" tab . To set the property to "org.w3c.flute.parser.Parser" as suggested by javamonkey79, you would specify:
-Dorg.w3c.css.sac.parser=org.w3c.flute.parser.Parser
It looks like you need to provide an interface implementation. Per this link here, this is one way to do it:
get the flute parser here
add it to your build path
add this to the DemoSAC class (before the getProperty call):
System.setProperty("org.w3c.css.sac.parser",
"org.w3c.flute.parser.Parser");
I don't know why it is doing this reflection based instantiation to begin with, perhaps bypassing ParserFactory and returning a new instance of the flute parser would be cleaner altogether?