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) 
5
  • 1
    Which directory are you in when running from the command line? When you open a file without the full path, it'll use the current working directory to look for it. Commented May 21, 2013 at 21:27
  • 1
    Is the property file in the jar? Commented May 21, 2013 at 21:28
  • I copied the jar file c:/users/javaMan directory Commented May 21, 2013 at 21:30
  • yes property file in DummyFilePath.jar Commented May 21, 2013 at 21:47
  • 1
    This is not a duplicate, as the suggested question discusses accessing a file contained within the .jar, while this question concerns accessing something in the file system. Commented May 22, 2013 at 14:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But i cannot give the absolute path as it will be used in some other machine
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.)
0

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

5 Comments

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 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.
I am in the same directory where the jar file is present. still it shows the same error.
Where exactly is it in jar...is it in the root?
yes it is in the root

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.