.java file path to class name - java

What's the most efficient way of getting the class(es) created on a .java file? I have the .java file path and I need to get the class full name.
I can only remember:
Compile it with JavaCompiler
Using the file text to parse it with Eclipse ASTParser (or another)
Infer the class name through part of the file path, but I don't know
if this works for every cases
Somehow, use a tool like javap (dind't really thought about this one)
EDIT
I have this file, located at C:\myfolder\MyClass.java (let's ignore package and folder association conventions):
package mypackage.mysubpackage;
public class MyClass
{
// my class implementation here
public class MyInnerClass
{
// my inner class implementation here
}
}
The full name of the classes declared in this file are:
mypackage.mysubpackage.MyClass
mypackage.mysubpackage.MyClass.MyInnerClass (I don't know if this
one it's correct, but let's pretend it is)
How can I get those class when I only have the .java file path (C:\myfolder\MyClass.java) ?

The only way to reliably obtain the names of the classes (mind that it may also define interfaces) files a .java file declares would be to really parse the java language contained in that file.
And even then you will need to know which compiler will be/has been used to compile the .java file, as a java compiler could use any naming convention it likes for anonymous classes (the Oracle compiler uses $1, $2..., but there is no strict need to mimic that behavior).
Considering these obstacles I believe its very hard to do from the .java files contents and simply impossible with the .java files path alone.

The most effective way is Class.forName().getName()

I have the .java file path and I need to get the class full name.
Which means, you know the path of .java file and you want the class name of each class file.
class Filter {
public static void main(String[] a) {
Filter f = new Filter();
String dirName = "D:\\Yourfolder\\"; // assuming your java file are located in D:\Yourfolder\
f.finder(dirName); // call the method for listing all the class file
}
public File[] finder(String dirName) {
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename) {
if(filename.endsWith(".class"))
{
System.out.println(filename);
}
return filename.endsWith(".class");
}
});
}
}
Replace dirName with your .java directory path.

One approach is to scan the directory tree where your Java source files are located, and for each file ending in ".java", you take its full folder path as a String and convert each dir separator to a '.' character. This will give you the fully qualified class name (FQCN). For example, if the path is: com\foo\fee\Foo.java, that becomes com.foo.fee.Foo.
Of course, this does not give you inner or nested classes and other advanced things, but these are created when you compile.
I have seen this kind of directory scanning in many frameworks, even Spring.
I am working on this in Groovy, so far I have:
File file = new File(rootSourcePath)
file.eachFileRecurse(FILES){
def path =it.getAbsolutePath()
println path
if(path.endsWith(".java")){
// to do the conversion here
}
}
Hope this interpreted your question correctly.

To get the name of the class file Try this
void printClassName(String classname)
{
System.out.println("The class name " + classname +" is " + classname.getClass().getName());
}

Related

How to read the files inside a directory when Only Relative path of directory is known

In my java application,
1. I have relative path of the directory (Directory and files in it are part of the build).
2.The directory contains multiple files.
3. I want to read the file names in the parent directory.
4. Files can change later and are many in number, So I do not know the names of the files and Do not want my code to change if more files are added or removed or renamed
Now as I do not know the names of the files before hand as they may change later (there are multiple files which can vary according to environment). I only know about the relative path of the parent directory of the files.
How do I read the files ?
You can get list of all files of that directory by file.getlistFiles() method of file class.
It returns an array of files.
Even you can define filter for your files, so it returns exactly files that you want.
try {
File f = new File("D:/Programming");
FilenameFilter filter = new FilenameFilter() {
#Override
public boolean accept(File f, String name) {
// We want to find only .c files
return name.endsWith(".c");
}
};
// Note that this time we are using a File class as an array,
// instead of String
File[] files = f.listFiles(filter);
look at this example.
If you want to use relative path, You can use
System.getProperty("user.dir");
String relative Path =System.getProperty("user.dir");
it returns the folder that you put your app in it.
If your folder has some subfolders, you can simply use file.list();
it returns names of all files and folders of your directory .
String [] listOfMyFilesAndFolders =file.list();
you can add these names to your path to access another folders.
You can check your path is a file or is a folder by using
file.isDirectory();
for ( String path: listOfFilesAndFolders ) {
File file = new File(basePath+path);
if ( file.isDirectory() {
// it is a folder and you can use another for loop or recursion to navigate sub directories
} else {
// it is a file and you can do everyThing you want}}
I think that you can use recursion to walk in your sub directories use recursion to read more
I hope helps.
the question is badly asked and the text is misleading.
"I have relative path of the directory (Directory and files in it are part of the build)" means little and nothing, you have to clarify what you mean.
Assuming you get a relative directory (for example "/folder1/folder2") via command line parameter, you basically have 3 choices:
1) start the program with the directory in which the jar is located as the "current directory", so that it can be used as a working directory. This approach requires that when you launch your application via the java -jar myapp.jar command first you prepend a "cd" command to place yourself directly in the directory. The code will look like the #hamidreza75 solution but obviously you will not have the variable "D: / Programming" but directly the relative path of the directory in which to read the files.
launch script
#echo off
cd {jar-folder}
java -jar myapp.jar
java code:
package com.sample.stack;
import java.io.File;
import java.io.FilenameFilter;
public class FileRenamenter {
public static void main(String[] args) {
final String relativePath = "folder1/folder2";
File directory = new File(relativePath);
String[] list = directory.list(new FilenameFilter(){
public boolean accept(File dir, String name) {
// filter condition
return true;
}
});
// echo file list
for (String filePath : list) {
System.out.println(filePath);
}
}
}
2) pass the folder to be controlled via command line parameter, like this:
launch script
#echo off
java -jar {jar-folder}/myapp.jar {jar-folder}
java code (only the "directory" variable changes)
File directory = new File(args[0], relativePath); // note args[0]
3) programmatically find the folder in which the jar is running [very discouraged practice] and then concatenate the relative path:
java code (only the "directory" variable changes):
String jarFolder = ClassLoader.getSystemClassLoader().getResource(".").getPath();
File directory = new File(jarFolder, relativePath); // note jarFolder

Get path of binary file within a JAR file

I have a jar file when unzipped looks like the following:
models/
com/
com/github/
com/github/
com/github/test/linux-x86_64/
com/github/test/config/
models/model.bin
com/github/test/Test.class
In the Test class, I'm using the path of model.bin inside the getResults method like so:
public class Test {
public List<Label> getResults(String text, int k) {
// Load model from file
loadModel("models/model.bin");
// Do label prediction
return results(text, k);
}
}
The loadModel method above is actually c++ method that looks like this. I am writing a Java Wrapper around a c++ library:
void Testtext::loadModel(const std::string& filename) {
std::ifstream ifs(filename, std::ifstream::binary);
if (!ifs.is_open()) {
throw std::invalid_argument(filename + " cannot be opened for loading!");
}
if (!checkModel(ifs)) {
throw std::invalid_argument(filename + " has wrong file format!");
}
loadModel(ifs);
ifs.close();
}
Currently, when I run this from an IDE, I get an error that says the model.bin file cannot be found. How do I determine the path of the model.bin within a jar file?
It sounds like whatever loadModel is (it is not part of standard java, so you'd have to elaborate) requires an actual file. Which your model.bin resource isn't (it's an entry in a jar).
Check if loadModel has an InputStream based method. It really should; if it doesn't, whatever library this is, does not follow java conventions, and is probably badly written.
If it does:
try (InputStream in = Test.class.getResourceAsStream("/models/model.bin")) {
loadModel(in);
}
will do the trick.
If it doesn't, oh boy. Either fix loadModel yourself, ask its authors to fix it, or if you must work around it, your only option is to copy the resource into an actual file, presumably in a temp dir, and then use that temporary file. This isn't pretty.

Given only the full path to a .class file, how can I load its Class object?

I'm working on an application that needs to be able to do some analysis on Java code; some through the text and some through reflection. The user should be able to input a directory and the program will pull all .java and .class files from it. I have no problem with the .java files but when I try getting the class of the .class file it doesn't work.
I've looked at using ClassLoader with URL. What I've been able to find so far has given me this
URL url = this.openFile().toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(this.path);
return cls;
path is just a string containing the actual path of the .class file in question, e.g. Users/me/Documents/Application/out/production/MyPackage/MyClass.class. From what I understand from my own reading, this method ties me to knowing the package structure of the input, but in general I don't. All I have is the absolute path of the .class file. Is there a way, just using this path (or some simple transformation of it) that I can load into my program the actual MyClass class object and start doing reflection on it?
You have 2 options:
Use a byte code library to first read the file, so you can find out what the actual class name is.
E.g. in your example it is probably MyPackage.MyClass, but you can't know that from the fully qualified file name.
Implement your own ClassLoader subclass, and call defineClass(byte[] b, int off, int len).
Recommend using option 2.

Translate package+class name to .class filename

I'm writing a program which analyzes .class files. I want to attach package and class name to my output.
My plan is to write a function which takes the package and class name as input and finds the corresponding .class file (so the user doesn't have to enter that as well, and can't get it wrong), where 'find' should be read as 'give me some data I can use as arguments for BCEL's ClassParser constructor' (either a filename, or a name of a zip file and a name within the zip file).
How do I go about that? Does java come with something which does that? I understand name resolution to be done in the context of a CLASSPATH, so the user should probably supply one of those as well; that's fine.
Note: solutions should not execute any code from the .class file. Just the bytes, ma'm ;-)
Well, if you are using BCEL, everything is already there, see ClassPath:
import java.io.IOException;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.util.ClassPath;
public class BcelTest {
public static void main(String[] args) throws IOException {
String classPath=System.getProperty("java.class.path");
// demonstrating with our own class path examplary for an arbitrary path String
ClassPath cp=new ClassPath(classPath);
ClassPath.ClassFile cf=cp.getClassFile(BcelTest.class.getName());
ClassParser p=new ClassParser(cf.getInputStream(), cf.getPath());
JavaClass jc = p.parse();
System.out.println(jc);
// or just using our own system path explicitly
cf=ClassPath.SYSTEM_CLASS_PATH.getClassFile("java.lang.Object");
p=new ClassParser(cf.getInputStream(), cf.getPath());
jc = p.parse();
System.out.println(jc);
}
}
If you already have the classfile, just replace every / in the class name with the directory separator for your system and append .class.
If you want to go from Java class name to bytecode class name, most classes are pretty simple. Prepend the package + '.' and replace '.' with '/'. For inner and nested classes, I'm not sure if there is a simple algorithm.
I want to thank the fine folks in irc://freenode.org/##java for guiding me towards the following recipe:
// replace 'Throwable' with something else in production code :-)
JavaClass recipe() throws Throwable {
URLClassLoader loader = new URLClassLoader(new URL[] {
new URL("file:///usr/share/java/js.jar"),
});
String path = "org/mozilla/classfile/ByteCode.class";
InputStream resource = loader.getResourceAsStream(path);
String fake_filename = "<" + path + ">";
ClassParser classparser = new ClassParser(resource, fake_filename);
JavaClass java_class = classparser.parse();
// System.out.format("%s\n", jc);
return java_class;
}
One could de-hardcode the classpath and the value of path by computing strings in the right format based on user input.
I'm going to use the answer I have accepted because it takes less work on my part, but I want to mention this answer because it is more generally applicable (i.e. with fewer dependencies).

Get file full path in java

When I pass File file to a method I'm trying to get its full path like file.getAbsolutePath(); I always get the same result no matter which one I use either absolute or canonical path PATH_TO_MY_WORKSPACE/projectName/filename and it is not there, how can I get exact location of the file?
Thank you
DETAILS:
Here is some code and this solutions(its bad but its working):
private static void doSomethingToDirectory(File factDir) throws IOException {
File[] dirContents = factDir.listFiles();
if(factDir.isDirectory() && dirContents.length > 0){
for (int i = 0; i < dirContents.length; i++) {
for (String str : dirContents[i].list()) {
if(str.equals(TEMP_COMPARE_FILE)){
process(new File(dirContents[i].getAbsolutePath() + "\\" + str));
}
}
}
}
}
I'm looping trough directories where factDir is src/main, I'm seeking toBeProcessed.txt files only that is TEMP_COMPARE_FILE value and I'm sending them to process method which reads the file and does processing of it.
If someone could better solution I'd be greatful
This quote from the Javadoc might be helpful:
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
I interpret this so that if you create your File object with new File("filename") where filename is a relative path, that path will not be converted into an absolute path even by a call to file.getAbsolutePath().
Update: now that you posted code, I can think of some ways to improve it:
you could use a FilenameFilter to find the desired files,
note that list and listFiles return null for non-directory objects, so we need an extra check for that,
you could also use listFiles() again in the inner loop, thus avoiding the need to create new File objects with hand-assembled paths. (Btw note that appending \\ manually to the path is not portable; the proper way would be to use File.separator).
The end result is
private static void doSomethingToDirectory(File factDir) throws IOException {
if (factDir.isDirectory()) {
for (File file : factDir.listFiles()) {
if (file.isDirectory()) {
for (File child : file.listFiles(new MyFilter())) {
process(child);
}
}
}
}
}
class MyFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.equals(TEMP_COMPARE_FILE);
}
}
Note that this code mimics the behaviour of your original piece of code as much as I understood it; most notably, it finds the files with the proper name only in the direct subdirectories of factDir, nonrecursively.
I think there is a way it may help you if and only if the file is in the program directory.
first you get the program directory by :
new File(".").getCanonicalPath()
then :
if file is inside a specific directory like folder\\filename
the full path will be
(new File(".").getCanonicalPath() + "\\folder\\filename")
or if file is directly inside the program directory:
the full path will be
(new File(".").getCanonicalPath() + "\\filename")
i wish this answer help you :)

Categories