我有一个外部可执行的jar文件,我希望使用Eclipse编辑它的java文件 [英] I have an external executable jar file that I wish to edit the java files in it with Eclipse

查看:107
本文介绍了我有一个外部可执行的jar文件,我希望使用Eclipse编辑它的java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是当我将外部jar文件添加到项目中时,它不会将java文件放在src中。我做什么?

But when I add the external jar file to a project it doesn't put the java files in the src. What do I do?

推荐答案

Jar文件通常是您的java类的编译形式。该源可以包含在jar中,但很少有。因此,而不是预期的.java文件,您将看到.class文件,它们是代码的编译形式。如果你想编辑这些文件,你有几个选项

Jar files are usually a compiled form of your java classes. The source CAN be included with the jar, but it rarely is. So instead of the expected ".java" files, you'll see ".class" files, which are compiled forms of your code. If you want to edit the files, you have a couple of options

是你最好的打赌真的。如果它是一个开源项目,寻找它的来源。您可以很容易地google,或在搜索引擎中搜索maven存储库,如这一个。例如,如果您看到此处,我看了对于junit,并找到一个名为sources.jar的条目。

如果它不是一个开源项目,而是您工作内部的一个项目,则可能是一个负责该项目的小组。接触他们,因为得到原始的来源,因为它们最初编译的是真正的最佳选择。

This is your best bet really. If its an open source project, look for the source of it. You can probably google it pretty easily, or search in search engines for maven repositories like this one. For example, if you see here, I looked for junit, and found an entry called "sources.jar".
If its not an open source project, but a project internal to your work, there might be a team in charge of that project. Reach out to them, as getting the original sources as they where originally compiled is really your best choice.

但是,如果你绝对不能访问源代码,该怎么办?那么你有两个选择。如果您非常熟悉Java,我只会推荐这些选项,因为它们可能会导致比解决问题更多的麻烦。

But what if you absolutely do not have access to the source code. Well then, you have two options. I would only recommend these options if you are VERY familiar with Java, as they can cause a lot more trouble than they can solve.

我假设你想这样做的原因是编辑其中一个类的一小部分行为,然后在你的项目中使用那个改变的类你可以访问代码;并且您要扩展的课程不是 final 。在这种情况下,您可以扩展该类,创建子类和然后覆盖控制您要更改的小功能的方法

I'm assuming the reason for you wanting to do this is to edit a small part of the behavior of one of the classes, and then use that changed class in a project of yours that you DO have access to the code in; and that the class that you want to extend isn't final. In this case you can extend the class, creating a subclass and then override the method that controls that small functionality you want to change.

现在,如果你不在乎你在做什么,这可能是一个非常危险的想法。原因是如果您无法访问代码,那么您无法确定实施细节的工作原理。哪些可能会导致错误。原始代码的可见性不足可导致

Now, this can be a pretty dangerous idea if you aren't careful with what you are doing. The reason being is that if you don't have access to the code, then you cannot know for sure how the implementation details work. Which might lead to errors. The lack of visibility to the original code can lead to


  1. 以类似的原始功能的方式重写方法。可能会发生这种情况,因为您重写的方法可能已经在执行某些内部操作,这些操作可能不明显。

  2. 原始类中可能存在非公开或不受保护的您的新课程可能无法访问。所以你可能无法用所需的功能完全替换功能。

  1. Overriding a method in such a way that the original functionality of the class is broken. This may happen because a method that you overwrote might have been doing certain internal actions that might not be obvious to you.
  2. There might be fields and methods on the original class that are non public or non protected which your new class may not have access to. So you may not be able to fully replace the functionality with the functionality desired.

那么,如果所有这些原因为什么不会做最好的主意,担心你吗?还有一种方法可以直接在类上替换功能。

So, what if all those reasons why this might not be the best idea do worry you? There is one more way to replace functionality directly on the class.

有很多Java可以使用.class文件的反编译器,并将其转换为.java文件,一些甚至与日食集成。但是,这比获取原始来源具有严重的缺点。

There are many Java decompilers available that will take a ".class" file, and convert it to a ".java" file, some that even integrate with eclipse. However, this has severe disadvantages over acquiring the original source


  1. 可能不合法。如果您拥有的罐子是由许可证提供的,那么在下载和使用所述罐子之前,您很可能会违反您同意的许可协议。

  2. 代码不会很简单读书。您将会放弃原始包含在源代码中的大部分漂亮的命名约定,使代码很难阅读。

解压缩和recompilinig可以很容易地导致比它的价值更多的错误。它完全有可能使用好,但如果可能,我建议你尽量远离。

Decompiling and recompilinig can very easily lead to more bugs than its worth. Its entirely possible to use well, but I'd advice you to stay away if possible.

那么你该怎么办?

我猜,现在你有一个jar,它提供了几乎漂亮的类(或类的列表)你想要什么,但不完全是那么,而不是更改原始源(你似乎没有访问权限)你使用该类,并创建一个方法来将它的最终组件更改为你想要的。您可以通过创建一个包含所需类作为字段的类,并对其进行修改。例如,让我们说你的罐子是一个叫做EuropeanCar的类,你喜欢它,但是想要一个AmericanCar,而唯一的区别是美国的汽车以Miles per hour的速度返回目前的速度,而不是EuropeanCar的公里每小时。但是所有其他功能在这个例子中是百分之百。

I'm guessing right now you have a jar, that provides a class (or list of classes) that does almost pretty much what you want, but not exactly. Well, how about instead of changing the original source (which you don't seem to have access to) you use that class, and create a method to change it's final components to what you want it to be. You could do this by creating a class that contains your desired class as a field, and have it modify it. For example, lets say your jar a class called EuropeanCar, and you like it, but want to have an AmericanCar instead, and the only difference is that the American car returns its current speed in "Miles per hour" instead of the EuropeanCar's "Kilometers per hour". But all other functionality is 100 percent the same in this example.

你可以这样做:

public class AmericanCar
{

    //...
    private EuropeanCar internalEuropeanCar;

    //...
    public double getCurrentSpeed()
    {
        return AmericanCar.convertKilometerToMiles(this.internalEuropeanCar.getCurrentSpeed());
    }

    private static double convertKilometerToMiles(double currentSpeed)
    {
        // ...
        return CONVESION_VALUES;
    }   

    //...
}

这是一个过于简化的例子,但是想法是使用组件和这些组件可以是在jar中提供的类型,供您在程序中使用。这是迄今为止最简单的解决方案,如果您无法访问原始源代码,即使您这样做,也可能是一个更好的选择,具体取决于您要做的更改。

That is an overly simplified example ofcourse, but the idea is to use components, and these components can be of the type provided in the jar for you to use in your program. This is by far the cleanest solution to your problem IF you don't have access to the original source code, and even if you do, it might be a better choice depending on the change you want to make.

这篇关于我有一个外部可执行的jar文件,我希望使用Eclipse编辑它的java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆