?
Solved

include source fragments in java compile?

Posted on 2013-12-18
10
Medium Priority
?
282 Views
Last Modified: 2013-12-20
Can I include a reference to a java source code fragment in a .java file? I.e. like an include or copybook? I'm looking for the equivalent of the jsp: include file="path" functionality.
0
Comment
Question by:jmarkfoley
[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
  • 5
  • 4
10 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 1500 total points
ID: 39728391
In a word... No.

Are you able to post the code fragment, and what you want to "include" it in? That way we have a better idea aobut what you are trying to do and we can perhaps guide you as to the usual way that this might get implemented.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39728410
I have no specific example. sometime I have a several line fragment I used in various programs, such as the database error reporting for a try/catch that prints out a standard message. Right now, I just copy/paste the same lines. It's not worth trying to put that in a jarfile. I suppose, now that I've mentioned it. the try/catch might be a good example, these exact lines are repeated whenever I do a database query:

   try {
        rs = stmt.executeQuery(query);
    }
    catch ( SQLException sqe) {
        System.err.println("Select Failed: " + sqe.getErrorCode() + " " + sqe.getMessage() + "\n" + query);

        if (rs != null) rs.close();
        con.close();
        System.exit(sqe.getErrorCode());
    }

A common include would be nice for this example in case I ever decide to globally change the error message.

Too bad, includes/copybooks can be useful. Many languages have that feature. I suppose I could create my own pre-processor ...
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39728432
It's not worth trying to put that in a jarfile
Fairly subjective, but I would say that it could be worth it. I guess it just depends how often you do use it. But especially, since you said that you may want to change the message in the future. I don't see that setting up a JAR to hold this is much more effort than having a file and including it in every place that you use it.

The other alternative that is half-way inbetween setting up a JAR for this and just copy/paste, is using features of your IDE (if you are using one). Eclipse for example, has a "Templates" feature where you can insert a block of code that you can easily call up using Content Assist. The end result is the same as copy/paste, so you would get any advantage like being able to change the message globally, etc, but it is much quicker than finding the block of code elsewhere and doing the copy/paste. If you are interested in this and you're using Eclipse, it should be fairly easy to figure out, otherwise just post back here and we can guide you further.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 39728445
Not using an IDE, using emacs on Linux. However, I could, if I wanted, create an emacs macro that would insert the lines at the cursor, similar to your suggestion ... but not really what I was after.

As to the jar function, I'd have to pass the rs and con to the function, doubtful I could (or should) do the System.exit() from the function. So, I'd be replacing, at best, 3 lines with 1, not really worth it considering I also have to configure the classpath, import, etc. -- which, frankly, I haven't quite mastered with respect to creating jarfiles.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39728685
After having tried (and failed) to make the template snippet reusable at the (proper) binary level rather than the source level, macros are your friend. I use them all the time, but with vim rather than emacs
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 39730086
Thanks!
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39730833
Thanks for accepting, just wondering though why the 'B' grade? You may not have seen the following article (http://support.experts-exchange.com/customer/portal/articles/481419) but it is generally what is followed here on EE.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39730935
Sorry, I guess I generally go "B" if I don't get what I want -- not to say what I want is possible. I didn't realize there was a point multiplier involved. I you want to request a change to "A" I'd have no objection. (Awarding points less than a 500 total and grades less than A, given the point-multiplier scenario, really doesn't make much practical sense. People aren't going to even mess with low point questions. EE should just get rid of these options)
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39730975
I pretty much agree with all your points above! :)  It's probably more a hangover from the early years of EE where things (as far as I know) worked a bit differently.

As for this question, lets just leave it be... I'm not too concerned, just good to know for the future! :)
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 39732070
I've been using EE regularly for 9 years, but don't really notice "conventions" unless someone points them out. I've always used the A/B/C grading to reflect the usability of the solution with A meaning "solved my problem" and B meaning "didn't or couldn't solve my problem, but got some definitive info." I don't think I've ever used "C". My intent was to give potential searchers finding this solution a bit of a heads-up about "solvability". Like I said, I didn't realize it affected points. I don't really see its usefulness in that sense. Anyway, nothing but A's from now on! Thanks for pointing that out. I've probably irritated people in the past.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

762 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