Solved

include source fragments in java compile?

Posted on 2013-12-18
10
276 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 67
xampp tool 12 50
tomcat not starting 6 45
Problem to Alipay 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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