-1

i have a ready to use .jar file and want to know if its possible to extract and rename the packages?

so when usually i start the .jar file with:

java -cp myFile.jar com.codehelper.demo.Main

i want to rename the "codehelper" in it to something different that i can run it by

java -cp myFile.jar com.NEW_NAME.demo.Main

i tried to decompile all files, add it to the folderstructure with renamed "codehelper" path and compile it again but it didnt work. i also renamed all the package includes in each file like

import com.codehelper...

so is my goal unreachable or can i do this? and if someone can explain me how to do, it will be very nice.

thank you and sory for my poor english

edit: it seems the only file i cant compile is a file containing this switch case.

  private int priotiryLevel(DiscoveryInfoBehave info)
  {
    int ret = 0;
    switch (1.$SwitchMap$com$peerialism$natcracker$common$GatewayDevice$GatewayType[info.getNatDevice().getGatewayType().ordinal()])
    {
    case 1: 
      ret = 0;
      break;
    case 2: 
      ret = 4;
      break;
    case 3: 
      ret = 5;
      break;
    }
    return ret;
  }

i tried also to rename the specific word inside this switch case but no effort.

muni
  • 119
  • 13
  • 4
    **How** didn't it work? What you're doing is theoretically possible, but the main problem is that the code might contain class and package names as strings and could use reflection to dynamically load classes and resources, assuming the original package structure. Note that you shouldn't have to decompile code. That means you don't own it, otherwise you would have the source files. That also means decompiling it might very well be illegal. – JB Nizet Feb 14 '15 at 07:31
  • it didnt work in the case that one file seems to make trouble, i think thhats a "dinamic loader" you talkes about. i added it to my question maybe you can see it. – muni Feb 14 '15 at 07:54
  • 1
    The question to this problem is WHY? What you gain by renaming classes? – Martin Perry Feb 14 '15 at 07:59
  • This simply shows that your decompiler is unable to correctly decompile switch statements on enums. – JB Nizet Feb 14 '15 at 08:00
  • so you think i can find my solutionn in another compiler? i found this page right now http://www.benf.org/other/cfr/switch-on-enum.html – muni Feb 14 '15 at 08:01
  • @MartinPerry - because it looks more serious having my own name in the file.. – muni Feb 14 '15 at 08:26
  • @muni would look even more serious if you a) had the source code and did the change correctly (i.e. in the source, then recompile cleanly) and b) if it didn't have several chances of, even if compiling correctly -- after decompiling --, not having the expected behaviour (as in `Class.forName` usages). as @MartinPerry asks, why do you need this if you don't have the source code? Why do you need to increase the "seriousness" by this means? – h7r Feb 14 '15 at 08:40
  • having source code IS serious, it doesnt LOOK serious, you didnt get the difference? i use open source code in my own project but dont want to see it everytime i invoke my own program – muni Feb 14 '15 at 08:42
  • You don't have any source files to rename. – user207421 Feb 14 '15 at 08:47
  • thank you i already know that. i decompiled them and renamed the package statements and directory names.. – muni Feb 14 '15 at 08:50

2 Answers2

0

Write a new wrapper class:

package com.NEW_NAME.demo;

public class Main
{
    public static void main(String[] args)
    {
        com.codehelper.demo.Main.main(args);
    }
}

compile it and add it to the jar. You can now invoke it as:

java -cp myFile.jar com.NEW_NAME.demo.Main

and it will silently dispatch to the real implementation.

Joe
  • 29,416
  • 12
  • 68
  • 88
  • damn, nice idea. i dont really know right now how to do this but i understand what you want to tell me, tnx a lot i'll give it a try. but when i crate a wrapperclass containing package com.NEW_NAME.demo; i cant put it in the same directory, right? where to put it then? – muni Feb 14 '15 at 08:27
0

This is not possible generally. If you rename some files, Java wont be able to find them (public class name must be same as file name). You can rename file with main class and call it as @Joe suggested in his answer. But if you rename somthing else, it will stop working. There could be cals to those classes in code. Same goes for the "codehelper" name. You can not remove it from code. Even if you remove it from one file, anyone will still be able to see this somewhere else in the code.

Rename directory is the same as rename file. You destroy namespace (package) and code will no longer work, because inside classes, this package is used. Plus there is no need to have import in the code, since you can call directly com.package-name.class from the executive code. By renaming package, you will destroy this and program will crash. It may run for a while, but once the program reach to this call, it will crash.

So this

import com.codehelper...

is not mandatory in the code, even if the code is using the package. You can write directly

com.codehelper.*** xy = new com.codehelper.***();

Even if you rename everything in the code, you still dont have guaranted functionality. Code may be using reflection and create class instances from sting code. For example see this:

Java how to instantiate a class from string


Under the line comment:

Doing some decompile -> compile work, is seems like code stealing, if you are not willing to pay licence and you want to hide it.

Plus doing something like this, it is ALWAYS a bad practice. I dont see any real use for this.

Community
  • 1
  • 1
Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • i dont want to rename classes, just the directory names and the package statements inside the classes that i can invoke the programm with i.e com.demo.main instead of com.peerialism.natcracker.discovery.Main – muni Feb 14 '15 at 08:49
  • but i also renamed all package statements inside the class files so the files should be found i think. isnt it? – muni Feb 14 '15 at 08:54
  • humm ok i didnt get it.. damn.. but thanks anyway :D – muni Feb 14 '15 at 08:57