Solved

include source fragments in java compile?

Posted on 2013-12-18
10
273 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now