5

I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements

String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));

where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package

The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.

However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.

And of course when someone tries to add those source files to be used automatically as resource files the process fails.

Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.

djechlin
  • 59,258
  • 35
  • 162
  • 290
Yannis P.
  • 2,745
  • 1
  • 24
  • 39
  • It is just a matter of ensuring the source is inside the Jar that Eclipse makes. If it is, it will work. – Andrew Thompson Dec 07 '12 at 14:51
  • Thank you @andrew-thompson. I have ensured it and it works inside the jar but not when running directly from Eclipse. And as you can understand it is not like a regular let's say .txt file. The .java files are not copied by Eclipse to the /bin/ directory! – Yannis P. Dec 07 '12 at 14:54
  • *"it is not like a regular let's say .txt file."* A Java source is usually a particular encoding and the extension is different, and while it will have 'Java code' inside it, it is very much like a text file to a computer (or software). – Andrew Thompson Dec 07 '12 at 14:57
  • 2
    Not an exact duplicate, but may help http://stackoverflow.com/questions/5607723/copying-data-files-upon-build-in-java-eclipse – Jacob Schoen Dec 07 '12 at 14:58
  • @jschoen I think it is very much on-topic. Good call. +1 – Andrew Thompson Dec 07 '12 at 14:59
  • @Andrew Thompson No doubt about that. What I meant is that whereas if you create a .txt file inside a package this is treated as _resource_ and will be copied to the `/bin` directory. However, how can you use you own .java files as resources, inside Eclipse? Within the .jar it works though... – Yannis P. Dec 07 '12 at 15:03
  • 1
    @YannisP. Not sure, but confident that playing with the defaults of eclipse (I think you can choose to have source & class in the same path) or writing an Ant build file should do it. – Andrew Thompson Dec 07 '12 at 15:06
  • Thanks for the suggestions @AndrewThompson and jschoen. I have actually followed Andrew's path. See edit for the details – Yannis P. Dec 07 '12 at 15:33
  • Please enter the edit as an answer and (when you can) accept it. Glad you got it sorted. :) – Andrew Thompson Dec 07 '12 at 15:36

1 Answers1

0

I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:

On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.

This causes class files be compiled in the same directory as the .java files and finally does what I wanted.

Thanks to @AndrewThompson for suggesting to try this

Yannis P.
  • 2,745
  • 1
  • 24
  • 39