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 6661573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:17:48+00:00 2026-05-26T02:17:48+00:00

if I define a java class in package a.b.c, but I just put the

  • 0

if I define a java class in package a.b.c, but I just put the compiled class file in c:\
, and using a URLClassloader to load it, will there be errors ?

Edit———————————————————-

package amarsoft.rcp.base.util.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class JavaCompolierDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String source = " package a.b.c; public class Test { public static void main(String args[]) {     System.out.println(\"hello\"); } }";

        // Save source in .java file.
        File root = new File("C:\\java\\");
        root.mkdir();
        File sourceFile = new File(root, "\\Test.java");
        Writer writer = new FileWriter(sourceFile);
        writer.write(source);
        writer.close();

        // Compile source file.
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null, null, null, sourceFile.getPath());
    }

}

when I run above code, the defined package a.b.c will not be generated as directory a\b\c, the Test.class will be in c:\Test.class

  • 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-05-26T02:17:48+00:00Added an answer on May 26, 2026 at 2:17 am

    As you know when you try to load a.b.c it will looking a directory a/b for a file c so if the file is not there, it won’t find it.

    If you change the URLClassLoader to look for the file in another directory, it can load the class without error.

    What are you trying to do?

    EDIT: Here is an example for a small compiler library I wrote a few years ago. It takes the source from a String in memory and gives you the outer class compiled.

    http://essence.svn.sourceforge.net/viewvc/essence/trunk/essence-file/src/test/java/org/freshvanilla/compile/CompilerTest.java?revision=293&view=markup

    This uses the Compiler API to do this. If you want to debug the code it will write it to a directory you specify (so you can step through the generated code) otherwise it works entirely in memory.

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

Sidebar

Related Questions

In Java you can define generic class that accepts only types that extend a
In Java, you can define multiple top level classes in a single file, providing
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
In the Eclipse Helios Java Package Explorer, I see the Java class icons display
in the java test I have : package Tester.GUI.api public class Test1{-----} in the
In Java when you compile a .java file which defines a class, it creates
Actually I am using the Rhino plugin in Eclipse. To use a Java class
This package has a class - HowToUseGeocell.java - with test cases defined. I have
While you create a user defined class in Java, you do not specify it
Does anyone know if it possible to define the equivalent of a java custom

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.