-3

Suppose i have a file named DevelopmentFeeCalculator in the project folder, now i want to get the class name of this file

class main  {

    public static void main(String [] args) throws Exception
    {           
    String className = System.getProperty(DevelopmentFeeCalculator.class.getName());

    String
     className1 = System.getProperty("user.dir.DevelopmentFeeCalculator.class.getName()");

    String
     className2 = System.getProperty("DevelopmentFeeCalculator.class.getName()");

         }
    }

none of them works.

  • 1
    You don't need `System.getProperty`, use just `DevelopmentFeeCalculator.class.getName()` –  Mar 11 '17 at 22:02
  • 1
    but my Instructor told me to use System.getProperty() – Ahsan Habib Himel Mar 11 '17 at 22:23
  • What exactly do you need to do? If you need the class name, using system properties is not needed –  Mar 11 '17 at 22:24
  • I have got a sheet and My faculty draw a diagram where he shoed that we have to use System.getProperty function. I tried many wways but nothing happened. I dont know whether he will accept my project if i dont use this function – Ahsan Habib Himel Mar 11 '17 at 22:30
  • You can use system properties for many things, but you don't need it to find the name of a class. I suggest you start from the beginning: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html –  Mar 11 '17 at 23:23

2 Answers2

0

Well System.getProperty is not for that porpuse

Here you can read this:

https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

If you want that you just to use:

 String className = System.getProperty(NumberStatistics.class.getName());
    System.out.println(className);
    String classNameCorrect = NumberStatistics.class.getName();
    System.out.println(classNameCorrect);

null reader.NumberStatistics

hope it helps

Yussef
  • 610
  • 6
  • 11
0

Ok well as i said I don't know why you want to use that, i think maybe you want to read the path from the file or define something a value you know what i mean?

So if you wanna do it in that way you will need to add in your Property a new key and value like this:

package reader;

public class Main {
    public static void main(String[] args) {
        String key= Main.class.getName();
        String value = key;
        System.setProperty(key, value);
        System.out.println(System.getProperty(key));
    }
}

I got: reader.Main

As you can see Now i'm using what you wanted a key and i got the value ;)

Hope it helps

You can read a little bit about this here

How to add new System Properties in java

Community
  • 1
  • 1
Yussef
  • 610
  • 6
  • 11