Avatar of Mark
Mark

asked on 

include source fragments in java compile?

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.
Java

Avatar of undefined
Last Comment
Mark
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mark
Mark

ASKER

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 ...
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of Mark
Mark

ASKER

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Mark
Mark

ASKER

Thanks!
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of Mark
Mark

ASKER

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)
Avatar of mccarl
mccarl
Flag of Australia image

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! :)
Avatar of Mark
Mark

ASKER

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.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo