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

  • Home
  • SEARCH
  • 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 9016961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:08:27+00:00 2026-06-16T04:08:27+00:00

I have this problem that i have a program that writes and creates a

  • 0

I have this problem that i have a program that writes and creates a .java file and puts it in my package folder, after this it takes the information from the .java file and uses it in it self. (it creates a new class with a method that i then import).
The problem is that if it wont work until i with eclipse update the “self created file”. is there a way to make my main file update the “self created file”.

Sorry if this is a duplicate. I just couldn’t find it any where.

my code:

package dk.Nicolai.Bonde;
import java.io.*;

public class main {

public String outputString ="Math.sqrt(25)" ;
static String outputPath ="src/output.txt";
/**
 * @param args
 * @throws UnsupportedEncodingException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws IOException{
        new main().doit(args);
    }

public void doit(String[] args) throws IOException{
    PrintWriter writer = new PrintWriter("src/dk/Nicolai/Bonde/calculate.java", "UTF-8");
        writer.println("package dk.Nicolai.Bonde;");
        writer.println("public class calculate{");
        writer.println("public void calc(){");
        writer.println("System.out.println("+outputString+");");
        writer.println("}");    
        writer.println("}");
        writer.flush();
        writer.close();

    calculate calcObj = new calculate();
    calcObj.calc();

}
}
  • 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-16T04:08:29+00:00Added an answer on June 16, 2026 at 4:08 am

    Your main mistake is that you expected that it’s during runtime automagically compiled into a .class file after save (which a sane IDE such as Eclipse is doing automatically for you behind the scenes everytime you press Ctrl+S). This is thus not true. During runtime, you need to compile it yourself by JavaCompiler and then load by URLClassLoader. A concrete example is given in this related question&answer: How do I programmatically compile and instantiate a Java class?

    You’ll in the concrete example also notice that you can’t do just a new calculate(); thereafter. The classpath won’t be auto-refreshed during runtime or so. You’d need to do a Class#forName(), passing the FQN and the URLClassLoader. E.g.

    Calculate calculate = (Calculate) Class.forName("com.example.Calculate", true, classLoader);
    

    Your other mistake is that you’re relying on the disk file system’s current working directory always being the Eclipse project’s source root folder. This is not robust. This folder is not present at all when building and distributing the application. You should instead write the file to a fixed/absolute folder elsewhere outside the IDE project’s structure. This is also covered in the aforelinked answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started learning Java and am having problem with a simple program that
I have this code that basically writes to a file 5 times using IO
I have written a program with C#, that creates a logfile and fills this
I have this problem that I can ref a button for control it. I
I have this problem that my sites uses alot of ajax and when a
Ok I have this problem that I've never had before, it's really bugging me.
So we have this problem that we are trying to figure out. Heres what
I have this problem decomposing a relation schema into a set of schemas that
I have a problem that I can't resolve. Let's say I have this map
I have this annoying problem, that Im pretty sure happens to all of you

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.