Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Get location of main class file on disk

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Get location of main class file on disk

    I am rewriting one of my old AutoIt programs in Java

    AutoIt had a neat feature "At Symbol"ScriptDir (Google it to find the help file, I've already lost this post once to link restrictions). "At Symbol"ScriptDir functioned as a string containing the directory that the script was located at in the filesystem.

    I can't find the right search terms to google a similar feature for Java in google. I would greatly appreciate any help.

    Thank you.

    Note: the "At Symbol"s should be actual at symbols, but they trigger the link restrictions.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get location of main class file on disk

    For the java program to execute a class file, the file needs to be on the path referenced by the package/classpath.
    If the java program uses a custom classloader then I imagine the class file could be anywhere.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get location of main class file on disk

    I'm sorry I didn't say why I wanted to do this. I need it to be able to find, read, and write to a text file in the same directory.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get location of main class file on disk

    What I meant to describe was how code in a class file would find the directory the class file is located in. I assumed that is what you were asking.
    A combination of the path to the directory where the java command was executed and the classpath/package would point to the location of the class file.

    There are a lot of new classes and methods in the later releases. Perhaps one of them will give the value you want.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get location of main class file on disk

    Thanks, that was what I meant, I just misunderstood the first time, I'll try that.

  6. #6
    Junior Member
    Join Date
    Sep 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get location of main class file on disk

    Use following Code:
    package schachfiguren;
    import java.net.*;
    public class Locator 
    {
    	public URL getURL(String pfad)
    	{
    		URL url = this.getClass().getResource(pfad);
    		return url;
    	}
    }
    Where pfad could be 'Main.class' and Locator.class must be in the same Directory as Main.class.
    Last edited by MyOggRadio; September 20th, 2019 at 12:18 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get location of main class file on disk

    The call to getResource could be done from the class that wants to know its location. It wouldn't need to be in a separate class.
    If the class files are in a jarfile then there could be a problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 19th, 2014, 12:12 PM
  2. Specifying the file location
    By fahman_khan75@yahoo.com in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2014, 08:36 AM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. Execution of .jar file: Could not find main class....Error'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2012, 02:04 PM
  5. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM