• Post Reply Bookmark Topic Watch Topic
  • New Topic

setting classpath - one level down the current directory  RSS feed

 
saran harini
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we set the class path for one level down the current directory?
My structure is like
  • bin
  • lib
  • resources
  • hexicon.cmd
  • inside the folder dist.
    In this case I could run the .cmd file which has java -cp .;lib\;resources client.Test .
    But if i put the .cmd file inside bin as like this and run as
    I am getting this error Error: Could not find or load main class
    Is there any possibility to set the class path in this case
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can use .. to refer to the parent directory, so your classpath would become
    ..;..\lib\;..\resources

    saran harini wrote:java hexicon.cmd

    Is that really the command you are using ?
     
    saran harini
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry only hexicon.cmd

    I tried this one.. In my case a property files inside resources and it gives me error as:
    Caused by: java.io.FileNotFoundException: db.properties (The system cannot find the file specified)

    But i have specified resources in classpath and dont know why i get this error..
     
    Hauke Ingmar Schmidt
    Rancher
    Posts: 436
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Setting the classpath will not help you. The exception tells that the property file is read as file ("... new File("../resources/db.properties") and not as a resource ("... getResourceAsStream("/resources/db.properties"). That is a bad idea in most cases, as is apparent here. The code seems to expect a certain working directory relative to which the resources can be found. If no resource is found you would probably get a NullPointerException.

    Fixing the batch file to start the application or fiddling with the paths could work (e.g. a "cd .." at the start of the batch file). It still will be fragile and can break easily. Gaffa tape is not a valid solution when you are just starting.

    Better fix the code now and switch to resource loading instead of file access.
     
    saran harini
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.. now it works fine when i use this
    InputStream input = this.getClass().getClassLoader().getResourceAsStream("db.properties");
    instead of
    InputStream input = new FileInputStream("db.properties");

     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you need getClassLoader() in that call? What is wrong with getClass().getResource() or getClass().getResourceAsStream()?
     
    saran harini
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    when i use this : getClass().getResource("db.properties") I am getting this error at compile time

    [javac] C:\..\..\Conncetion.java:36: error:
    incompatible types
    [javac] InputStream input = this.getClass().getResource("db.properties");
    [javac] ^
    [javac] required: InputStream
    [javac] found: URL
    [javac] 1 error

    when i use this : getClass().getResourceAsStream("db.properties") I am getting this error at runtime:

    ....... File db.properties cannot be loaded
    Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    ... 5 more

    dont know the reason..
     
    Hauke Ingmar Schmidt
    Rancher
    Posts: 436
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you should qualify the resource with a path relative to the classpath, e.g. "/resources/db.properties" or "/com/example/application/db.properties". If you just use the name it will be searched in the same package as the class where it doesn't exist in this case.

    You shouldn't rely on the classloader more extensive search as you would need to elaborate which file is used when more than one exists (testing, debugging, attack on the app's credentials).
     
    saran harini
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!