?
Solved

Java Referencing file in eclipse project

Posted on 2014-01-15
7
Medium Priority
?
331 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
 

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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 86

Accepted Solution

by:
CEHJ earned 1800 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 36

Assisted Solution

by:mccarl
mccarl earned 200 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 1800 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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