2

I´m something new in Java Development and I will try to ask my question short and simple. Hope it works :-).

The Goal

Extend some Java classes from a given project without editing the original source files.

The Problem

It don´t work ;-)

Short description

I have two Java projects. Project A is a bigger Web project. Project B is a Web Fragment project. I want to extend some classes from Project A, but I don´t want edit the original sources. So the idea is to extend the classes in Project B.

Current state

Create Project B

I created Project B with the Eclipse Wizard

File > New > Project > Web Fragment Project

and add Project B to the Dynamic Web project, Project A

Add class to Project B

Now I copied the original Java class into Project B using the same folder structure as in Project A

Project A > WebContent > WEB-INF > src > a > b > c > MyClass.java
Project B > WebContent > WEB-INF > src > a > b > c > MyClass.java

And both classes are in the same package

package a.b.c;

So far so good. I write some test output for the console to check if the class will be overridden correctly.

Deployment

I deploy Project A and start the Tomcat Server. But my test output is not logged to the console. So I suspect, MyClass.java will be not overridden. Why?

Additional informations

Project A properties

  • Project B is in Web Deployment Assembly from Project A
  • I added Project B in the Projects Tab of the Java Build Path
  • Under the Order and Export Tab i move Project B over Project A

Question(s)

Why will the original class in Project Anot be overridden from MyClass.java in Project B. Have i something overseen?

Thank you in advance and best regards, Daniel

inxtremo
  • 41
  • 4

1 Answers1

1

You have to tell the JVM to use the patched class. In order to do that you match the package and class name for the patching class. The magic happens in the classpath. The JVM loads the class search in sequence. This means you set your patch to be first loaded e.g.

set CLASSPATH=..\yourPatch;..\theJar.jar

The Eclipse IDE has this as a built in function. You can find it

Project -> Properties -> Java Build Path -> Add Class Folder

Add your patched class as the first entry.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • @That's how I use it and it works. Maybe you did something wrong in the steps before. See here for more reading material https://stackoverflow.com/questions/33631419/replace-a-class-within-the-java-class-library-with-a-custom-version – Murat Karagöz May 29 '17 at 09:17
  • Thank you. I will try it further. – inxtremo May 29 '17 at 11:49