-1

how to prevent (programmatically) a java application from starting if it's not in a predefined directory?

Example:

I have application.jar and I want to make it runnable only if it's located in "C:\runnable" directory else the application will exit.

any ideas?

mwdar
  • 141
  • 2
  • 3
  • 10
  • *'I want to make it runnable only if it's located in "C:\runnable"'* So you want this to run only on Windows machines? What is the purpose of this extraordinary (silly) requirement? *"any ideas?"* Put aside the strategy you intend using to achieve the goal, and describe the goal. – Andrew Thompson Oct 01 '11 at 12:33
  • It's just An example. I'm no't even trying to make any applications, I never say my ideas to public. – mwdar Oct 01 '11 at 15:31
  • *"It's just An example"* A poor one, since it failed to communicate the goal. *"I never say my ideas to public."* (Rolls eyes.) If your idea has any value, it will be stolen within 24 hrs of becoming public, even without reverse engineering (e.g. de-compiling) the code. – Andrew Thompson Oct 01 '11 at 15:53
  • _"A poor one, since it failed to communicate the goal"_ Wrong! since "Radu C" gave me the perfect answer. – mwdar Oct 01 '11 at 17:17

4 Answers4

2
System.getProperty("user.dir").equals("C:\\runnable")
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
1
 final String classLocation = GetClassLocation.class.getName().replace('.', '/') 
  + ".class";

-> http://www.roseindia.net/java/java-get-example/get-class-location.shtml

Tobias
  • 9,170
  • 3
  • 24
  • 30
1

Can also Try -

    System.out.println(new File (".").getCanonicalPath()); // Current Path
    System.out.println(System.getProperty("user.dir")); // Path from where java is executed
Jayendra
  • 52,349
  • 4
  • 80
  • 90
0

If the actual criteria is whether the jar file is in a given location, you cannot rely on system properties, but must ask the JVM about the location of the jar file and then react accordingly.

See Determine location of a java class loaded by Matlab for instructions about how to determine the location of the byte code for a given class. Then do it for a class you know to be in the jar, and go on from there.

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347