Solved

include source fragments in java compile?

Posted on 2013-12-18
10
277 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
  • 5
  • 4
10 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 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 35

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 35

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 35

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
xampp tool 12 58
Tomcat: Unable to run tomcat service. 2 28
Chrome and Firefox Java 5 66
Netbeans and org.apache.commons.lang3 issue 3 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

680 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