.class file tweak inside traccar-server.jar

marc2 years ago

What's the easiest way 'rem out' a line of code in the Traccar server without all the complications?
I just want to add a simple ' // ' inside one of the modules inside the tracker-server.jar file, to block a particular function in one of the modules operating. The idea is do this instead of presently having to deny Traccar server MySQL permissions (which works OK, but has side effects).

I don't want to set up a github account to do a fork -to import into set up a Netbean Eclipse -to configure a SDK -to spend a year on a Java programing course -to compile a latest version -to locate missing dependencies to assemble a gradlew to debug compiling failures -to... -to...... I just want to rem out a tiny part of what's already there. Surely it can't be any harder than changing a byte. in a .class file then zipping it all back up again.

Anton Tananaev2 years ago

You're overthinking it.

  1. You can download source code as a ZIP from GitHub. You don't need an account or git tool for it.
  2. Then you can compile with a single command line gradlew build. It will download everything you need. You don't need to set up IDE or anything else.

It's much simpler to compile than editing bytes of compiled Java code.

marc2 years ago

I had tried the 'conventional approach' before but I'd found Github too complicated to use, it's full jam-packed with endless forks and options and links to other links ... OK I went to Github and surprisingly got https://github.com/traccar/traccar/archive/refs/heads/master.zip easily. In it are the source files that generate the server's tracker-server.jar : traccar-master.zip\traccar-master\src\main\java\org\traccar*.*

From here on I'm lost, I've no idea what a gradlew is and I suspect I have to do..xxx....in order to do..yyy...in order to do..zzz... to do.... as often happens with computers and software. I'll give it a try.

Sigh... what I had planned does seem simpler, open the .jar with Zip (.jar is a v1 zip file), edit the code bytes in the .class module, then put it back into the archive and attempt restart server.

marc2 years ago

Nope didn't work for me, Downloaded zip file, unpacked it, soon as I typed "gradlew build" and different variations on it all I just get errors like "Command 'gradlew' not found, did you mean:" which looks too much like a dependency thing when the tools aren't installed to do xxx... you have to to do yyy.... to do zzzz.... you have to do.... it's like having to tool up to rebuild an IKEA furniture from scratch just to change a shelf position why I think changing bytes in compiled code is be easier and definately much less complicated......

Anton Tananaev2 years ago

The gradlew executable is in the folder, but it doesn't seem like you're really interested in trying it the right way. Good luck with changing bytecode I guess.

marc2 years ago

I'd actually succeeded with the hex editing bytecode but I am alsso genuinely interested in doing the right way some day.

When I ran ./gradlew assemble in traccar-master I got a screen overflowing with errors "Cannot create.... Dependency..." etc ending up in "BUILD FAILED in 2s / FAILURE: Build failed with an exception". I instantly gave up because I'm no software engineer. I find mysellf these 'software build things' usually only ever work properly on the computer where the software was written on and there's often other added essential extras things needed that don't get documented.

Anyway, back to what I know works...., unzip tracker-server.jar.zip Use an online decompiler on modules to find what to look for. I then alter the relavent java .class file with a hex editor (the only tool I used). With it I transfer a single bit from one byte to another byte to corrupt the name of the function to stop it being called, while the same time that preserves the file checksum. Check the result with the same online decompiler. Then zip it all back to tracker-server.jar. Then sudo systemctl restart traccar and sudo systemctl status traccar to check it's running OK and then check to see how it worked out. If I had more time I would had altered program flow instead. So doing it properly of course I would prefer to do but that's beyond my skills at the moment...

What I've been doing? To stop Traccar Server deleting things. Traccar is unforgiving if people make human error. Delete a device and all of its position history suddenly dissappears with no way of restoring it back, (other than a custom script to parsing tracker-server.log info files and re-write the database entries again).

Other alternatives looked at:

  • run a secondary Traccar server as a backup (I described that on another thread). Advantage is there's no gaps in data after any 'restore from backup' because it's continually backing up data to the present time.
  • Remove Traccar's DELETE permission in MySQL permissions is another. But that has the disadvantage of stopping the auto delete of old position history.
marc2 years ago

More about the errors with the 'build from source' proper way. I tried
sudo ./gradlew assemble that had went further, but still resulted in fail BUILD FAILED in 4m 32s 5 actionable tasks: 5 executed and Execution failed for task 'compileJava' invalid source release: 11

I presume ./gradelew assemble = ./gradelew build is same thing

Anton Tananaev2 years ago

Do you have Java installed? That's one thing you do need, but it's usually installed on most computers anyway.

marc2 years ago
$ java --version
openjdk 10.0.1 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)
$ 

I don't know if that's good or bad. What I do know is I don't have your computer, not using same environment you use and I don't have your skills.

Anton Tananaev2 years ago

Traccar requires Java 11 or higher.

Java 10 end of support was September 2018 for OpenJDK, so I would definitely recommend upgrading, even if you don't plan to compile. There are probably known security issues.

In general you should really only use LTS releases of Java.

marc2 years ago

None of this is mentioned in Build from Source I followed the instruction to the letter and got a page overflowing with errors that went up the screen so fast I'd need a slow motion video recorder to replay it. Otherwise I'm just using whatever's there on my environment and I've not knowingly installed Java.

As I mentioned (well in my experience anyway) these 'build from source' things only work 100% properly on the machine the software's written on. Anything less it becomes an 'do xxx... to do yyy... to do zzz... to do...' un-predictable chain to try replicate the author's environment. Why I opt just make small hacks like I'd just did with the raw bytecode, so much easier :-)

I will have another go at the proper approach. So I have to install later version Java. I don't know how, I guess it's sudo apt install -- java or something, there'll be millions of pages on google how.

This is a good test of the Build from Source page.

marc2 years ago

Just a minute.... There must be Java installed, because the Traccar is already installed and running. I guess Java would have been installed on the same day Traccar was installed. I see a OS system file date Apr 20 2021 less than one year ago.

marc2 years ago

Traccar requires Java 11 or higher.

It seems I already have it...

$ java --version
openjdk 10.0.1 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

$ sudo apt install openjdk-**11**-jre-headless
Reading package lists... Done
Building dependency tree       
Reading state information... Done
**openjdk-11-jre-headless is already the newest version (10.0.1+10-3ubuntu1)**.
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
$ 

I did glimpse somewhere on a ORACLE Java download page that you need to get a key or activation code number or something to download later version, and also caught a glimpse on DigitalOcean website that in Ubuntu the Java has to be manually installed, and not through apt install...

Incidently, I'd also tried the Windows version of the Build with jdk-18 including the envirionment variables and PATH set , (you have to type ..\gradlew build) but here the Gradle server each time just cuts the connection part way through the first download because it takes too long to download their 108 Mb file, so this too fails. It might have worked if I had a faster internet connection. So that's it, I did try and it turned out in the end easier and less complicated to manually hack the .class file bytecode to get the result I'd wanted...