1

I have a simple project which depends on jar file. Jar file has single class with constructor which takes in path to props.xml.

This is the project structure:

Here is the main class:enter image description here

import com.file.reader.FileReader;


public class SimpleExample {
 public static void main(String[]args){
 FileReader rd = new FileReader("props.xml");
 }
}

Here is the FileReader.java

package com.file.reader;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class FileReader {


public FileReader(String fileName){
    try {

        File file = new File(fileName);

        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                                 .newDocumentBuilder();

        Document doc = dBuilder.parse(file);

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        if (doc.hasChildNodes()) {

            System.err.println((doc.getChildNodes()));

        }

        } catch (Exception e) {
        System.out.println(e.getMessage());
        }   

}
}

This basically reads the xml file.

FileReader.java is a jar file being accessed in my project. When i run in eclipse i see the below output:

 [#document: null]
 Root element :company

But when i exported the DummyFilePath as jar file and tried running from command line.

I see that error is being thrown:

 C:\Users\javaMan\props.xml (The system cannot find the file specified)

From Command line I am running

  Java -jar DummyFilePath.jar

How can i make it run through command line

EDIT

After checking some linked questions i tried a different way:

I moved the props.xml to src folder.

Then i changed the SimpleExample.java as below:

 import java.io.File;
 import java.net.URL;

  import com.file.reader.FileReader;

 public class SimpleExample {

public static void main(String[] args) {
    SimpleExample se = new SimpleExample();
    System.err.println(se.getPath());
    FileReader rd = new FileReader(se.getPath());
}
public String getPath(){
    URL url1 = getClass().getClassLoader().getResource("props.xml");
    File f = new File(url1.getFile());
    return f.getAbsolutePath();
}
}

So when i run in eclipse i see the below which is good:

 C:\Users\javaMan\Perforce\DummyFilePath\bin\props.xml
 [#document: null]
 Root element :company

When i run the same DummyFilePath.jar i see the below error:

C:\Users\javaMan\Desktop>java -jar "C:\Users\javaMan\Desktop\DummyFilePath.jar"
C:\Users\javaMan\Desktop\file:\C:\Users\javaMan\Desktop\DummyFilePath.jar!\props.xml
C:\Users\javaMan\Desktop\file:\C:\Users\javaMan\Desktop\DummyFilePath.jar!\props.xml (The filename, directory name, or volume label syntax is incorrect)
javaMan
  • 6,352
  • 20
  • 67
  • 94

2 Answers2

1

Since you only gave the File class a file name (indirectly through your constructor), it assumes you meant it was a relative path (relative to the current directory). In other words, it's equivalent to .\props.xml, and since your current directory on the command line is C:\Users\javaMan\ (which you can see at the left of your command prompt when you execute Java -jar DummyFilePath.jar), it looks there. You probably need to specify the absolute file path of props.xml.

For example, if props.xml is in C:\Users\javaMan\someotherfolder, the absolute path (in Windows, at least), would be C:\Users\javaMan\someotherfolder\props.xml.

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • But i cannot give the absolute path as it will be used in some other machine – javaMan May 21 '13 at 21:31
  • Then you need to either make it an argument or make sure it's in the current directory on any machine where you run your program. Whether it's worth the trouble of making it an argument depends on who will use your program. Note that, for example, if your program is in `C:\temp\javaManProgram` and that's not the current directory, this code will fail. In other words, it won't work if a user does this: `C:\temp> java -jar .\javaManProgram\DummyFilePath.jar` and `props.xml` is in `C:\temp\javaManProgram` with the program. (`C:\temp>` is the prompt, which is the current directory.) – jpmc26 May 21 '13 at 21:33
0

When exporting this , don't forget to add the resources. They are not always added, some builders filters resources.

docDevil
  • 72
  • 4
  • When i exported props.xml is selected it . I assume it has to do with relative paths. For eclipse it is taking current path as project directory. But for jar file it is taking the current path as path in which jar file lies. So it is looking for props.xml in the folder where jar file exists and failing – javaMan May 21 '13 at 21:48
  • 1
    @javaMan When executing the jar file from the command prompt, it should be using the current directory of the command prompt as the current directory. Try executing your jar file when your command prompt's current directory is somewhere other than your jar file. – jpmc26 May 22 '13 at 18:35
  • I am in the same directory where the jar file is present. still it shows the same error. – javaMan May 22 '13 at 19:06
  • Where exactly is it in jar...is it in the root? – docDevil May 22 '13 at 20:51
  • yes it is in the root – javaMan May 23 '13 at 13:46