0

so I have a pretty large project in Eclipse which runs fine and accesses files etc. And when in Eclipse I access files with a local directory name in relation to the root of the project directory.

So my project is called "Project1" for example and is inside a directory called "MyProjects" so it looks like this: "MyProjects/Project1". I want to access a file in the "MyProjects" folder called "hello.text". So I just do "../hello.text" and it works fine when I do this in Eclipse.

The problem I have is when I run the program using the command using "java Project1" it runs the program fine but it cannot access that file because when executing programs from the command line it stars them from the "bin" directory which is inside of the "Project1" directory. So it messes up the whole program. Is there anyway to change this easily in my Windows environment or Eclise? I hope my question makes sense. I want the program to execute from "Project1" directory if possible so I don't have to change the file location everytime.

Ray
  • 685
  • 3
  • 7
  • 15

3 Answers3

1

How about providing a directory argument so you don't have to worry about it at all ever?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • How so? This problem also affects a few jar files that I reference too. – Ray Sep 15 '11 at 22:49
  • How so? By normal Java command line argument means (depends on what your code is). What do you mean by "also affects a few jar files that I reference too"? – Dave Newton Sep 15 '11 at 23:13
  • In eclipse I just dragged the jar files right into the project so it configured them automatically. When I execute the program from the command line I'm getting a "java.lang.NoClassDefFoundError" for a class inside one of the jar files. It's actually "xstream" to be specific so I am getting an Eception in thread "main" java.lang.NoClassDefFoundError: "com/thoughworks/xstream/XStream" – Ray Sep 15 '11 at 23:45
  • You need to define your [classpath](http://en.wikipedia.org/wiki/Classpath_(Java)) properly; [here's one resource](http://download.oracle.com/javase/tutorial/essential/environment/paths.html). It'll be really important to understand classpaths. – Dave Newton Sep 15 '11 at 23:47
  • I checked out the article and verified it was set properly by running "java -version" and it showed the version properly. I can run programs fine I just cannot seem to access external jars, or how to configure them. – Ray Sep 15 '11 at 23:53
  • Ok I figured it out thanks for your help. I thought you meant to set the environment classpath. I included the classpath doing the command "java -classpath myjar1.jar;myjar2;bin Project1" to run the program. – Ray Sep 16 '11 at 00:06
1

You can use one of ClassLoader class methods

public URL getResource(String name)
or 
public InputStream getResourceAsStream(String name)

to locate and access any resource in the classpath in a way that is independent of the location of the code. For exaple

InputStream myTextFileStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Project1/Hello.txt") ;
mazaneicha
  • 8,794
  • 4
  • 33
  • 52
  • Would this also work for the external jar files that I have referenced? – Ray Sep 15 '11 at 22:52
  • Additional jar files need to be listed in the classpath, see the following SO topic: http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath – mazaneicha Sep 16 '11 at 03:44
0

Can't you start your Java program from the bin directory's parent?

e.g.

Project1> java -classpath bin MyApp

Does this answer for getting classpath from Eclipse help?

Community
  • 1
  • 1
Paul Grime
  • 14,970
  • 4
  • 36
  • 58
  • Your solution works but now for some reason now my jar file paths are not working. Maybe it's something with the build path? – Ray Sep 15 '11 at 23:13