Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:40:42+00:00 2026-06-14T10:40:42+00:00

I read files from disc in my Java project. I find my hohoho.java file

  • 0

I read files from disc in my Java project. I find my hohoho.java file on D:/ and it’s in File format, then I would like to add it as a class to existing package with my main class (Up.java). It should look like this – package -> Up.java, Hohoho.java.

And it should be done programmatically of course. I will use this .java file and it’s functions in my Up.java.

Do you know any easy way to do this?

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;

public class Up{

public static void main(String[] args) {

    File root = new File("..\\.");
    File myfile = null;

    try {

        String[] extensions = {"java"};
        boolean recursive = true;

        Collection files = FileUtils.listFiles(root, extensions, recursive);

        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
            File file = (File) iterator.next();
            String path = file.getAbsolutePath();
            System.out.println("File = " + path);

            String[] tokens = path.split("\\\\");

            for (String t : tokens)
              if (t.equals("Hohoho.java")){
                  myfile = file;
              }
            }

        System.out.println("Client class: " + myfile.getAbsolutePath());

    } catch (Exception e) {
        e.printStackTrace();
    }     

   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:40:43+00:00Added an answer on June 14, 2026 at 10:40 am

    If you only need to load a .class file, per your comment, you should be able to just use something like the following (assuming no security issues in your setup):

        String path = "/path/to/your/classfiles/root/"; //not including the package structure
        ClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + path)}, Up.class.getClassLoader());
    
        Class clazz = loader.loadClass("foo.Hohoho"); //assuming a package "foo" for that class
        Object loadable = clazz.newInstance();
        Field field = loadable.getClass().getField("someField");  //or whatever - (this assumes you have someField on foo.Hohoho)
    
        System.out.println(field.get(loadable));
    

    (I dropped all exception handling in the above). As @stemm pointed out, this is going to be painful to work with, just using pure Reflection.

    Also, a quick test of invoking the java compiler, in the least complicated way possible, from within the VM follows. So, if case you do need to go from source, you can build off this:

        String sourcePath = "/path/to/your/javafiles/root/"; 
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int success = compiler.run(null, null, null,  sourcePath + "foo/Loadable.java");//can't get "-sourcepath","path" to go, for some reason.
    
        System.out.println("success = " + success); //should be 0; Sys err should have details, otherwise
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is possible to read from a txt file in loop like this? files names:
i have csv files, java app and database, i read csv file from my
I want to read a file from a java web application. I don't want
Is it possible to read files from remote computers in JAVA and how?
I'm trying to read files from the directory. If file cannot be processed it
How to read files from bottom up in php?Is there something like stream in
Hi im using, FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); to read files from
I'm using a string builder to read several files from a directory like this:
I want to read files from a directory in matlab. Then I need to
Within a VB.NET program, I want to read files from a filesystem, then write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.