Solved

Java Referencing file in eclipse project

Posted on 2014-01-15
7
318 Views
Last Modified: 2014-01-16
Hello there,

Can somebody help me to understand how to reference a file in a folder. I have acctached my eclipse project structure. I am trying to refer a file in the templateprint folder in a java class in com.docmosis.sample.

I tried something like this but it cannot fine the file

TemplateIdentifier templateId = new TemplateIdentifier("/templateprint/exampleTemplate.doc);

Open in new window


cheers
ZOlf
ee1.gif
0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39781588
You can add your 'templateprint' directory as a source-folder (Project -> Properties -> build path > use as source folder).

InputStream ir = Thread.currentThread().getContextClassLoader().getResourceAsStream("exampleTemplate.doc");

It should work.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39781962
0
 

Author Comment

by:zolf
ID: 39782213
Thanks for the feedbacks.but in my code what do i need to do so my code will use that file.i mean what am i doing wrong in my code
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 86

Accepted Solution

by:
CEHJ earned 450 total points
ID: 39782484
Actually, since the (quite sparse) api docs suggest that the only TemplateStore implementation is file based, you can probably ignore my comments about resource loading. Instead, you should probably use file paths. The 'proper' way would be to configure that declaratively, with paths to stores being enterable into the app itself. If you want to provide a default, then you should probably be aware of the current directory and construct the path using java.io.File accordingly

http://technojeeves.com/joomla/index.php/free/132-find-the-current-directory-in-java

The composition of a TemplateIdentifier is something entirely unknown to me
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 50 total points
ID: 39784309
Not withstanding the facts above that in general you should provide proper file/path handling, if this is just an example program that you are trying to get going, this may work...
TemplateIdentifier templateId = new TemplateIdentifier("templateprint/exampleTemplate.doc);

Open in new window

I have removed the leading / so that it uses relative paths, and Eclipse should launch your program with the current directory set as your project directory, so it should be able to find it there.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 450 total points
ID: 39785277
and Eclipse should launch your program with the current directory set as your project directory, so it should be able to find it there.
That might be the case (i haven't checked the paths, nor know what the composition of that ID should be) but how an app runs from an IDE is irrelevant and using relative paths like that is fragile as (particularly in the absence of an installer) the current directory is not completely certain
0
 

Author Closing Comment

by:zolf
ID: 39787635
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question