Как пересобрать проект в intellij idea
Перейти к содержимому

Как пересобрать проект в intellij idea

  • автор:

idea how to recompile the project

The idea tool can be used to develop projects in multiple languages. If it is a programming language like java that needs to be compiled and run, the source code needs to be compiled every time before running the project. Under normal circumstances, idea automatically judges the modified files and then automatically compiles them, but sometimes we need to manually recompile the entire project. Here is how to use idea to recompile the project.

Tools/Materials

Method/Step

Use idea to open a project project, select the Build menu in the menu bar, and then select Rebuild project to recompile the project

Recompile the entire project. If there are many project files, the compilation process will take a long time. You can see the compilation status in the status bar at the bottom of the idea. Click the status to view the task list.

After the compilation is completed, you can view the compilation log details in the Messages Make window. If there are compilation errors, warnings, or expired messages, you can see them in this window and make corresponding modifications.

If the entire project file requires a long time to recompile each time, you can compile some modules, as shown in the figure, select one of the modules, then right-click and select Compile’module name’ to complete the compilation of the module. The speed is faster than the compilation of the entire project.

Many java projects now use maven to manage the project components, and you can directly use the maven compile plug-in to recompile the project. As shown in the figure, select Maven Projects, and then select one of the modules to open, select compile in Lifecycle and double-click to run the compilation

The compilation process and compilation results can be viewed from the running console. If there is no error, the [INFO] BUILD SUCCESS prompt message will be printed at the end

Precautions

Usually in the development process, the entire project is rarely compiled. Generally, the code idea of ​​a file is modified automatically and compiled. But if the project needs to be released, in order to ensure that it is the latest compiled package, the entire project will be recompiled.

Compile and build applications with IntelliJ IDEA

The IntelliJ IDEA compilation and building process compiles source files and brings together external libraries, properties files, and configurations to produce a living application. IntelliJ IDEA uses a compiler that works according to the Java specification.

You can compile a single file, use the incremental build for a module or a project, and rebuild a project from scratch.

If you have a pure Java or a Kotlin project we recommend that you use IntelliJ IDEA to build your project since IntelliJ IDEA supports the incremental build which significantly speeds up the building process.

However, IntelliJ IDEA native builder might not correctly build the Gradle or Maven project if its build script file uses custom plugins or tasks. In this case, the build delegation to Gradle or Maven can help you build your project correctly.

Compile a single file or class

Open the needed file in the editor and from the main menu, select Build | Recompile ‘class name’ ( Control+Shift+F9 ).

Alternatively, in the Project tool window, right-click the class you need and from the context menu, select Recompile ‘class name’ .

If errors occur during the compilation process, IntelliJ IDEA will display them in the Review compilation and build output along with warning messages.

Change the compilation output locations

When you compile your source code, IntelliJ IDEA automatically creates an output directory that contains compiled .class files.

Compile output directory

Inside the output directory, IntelliJ IDEA also creates subdirectories for each of your modules.

The default paths for subdirectories are as follows:

At the project level, you can change the <ProjectFolder>/out part of the output path. If you do so (say, specify some <OutputFolder> instead of <ProjectFolder>/out ) but don’t redefine the paths at the module level, the compilation results will go to <OutputFolder>/production/<ModuleName> and <OutputFolder>/test/<ModuleName> .

At the module level, you can specify any desirable compilation output location for the module sources and tests individually.

Specify compilation output folders

Open the Project Structure dialog ( File | Project Structure Control+Alt+Shift+S ).

In Project Settings , select Project and in the Project compiler output field, specify the corresponding path.

Project Structure dialog / Projects page

For modules, select Modules , the module you need and the Paths tab. Change the location of the output folder under the Compiler output section.

Build

When you execute the Build command, IntelliJ IDEA compiles all the classes inside your build target and places them inside the output directory.

When you change any class inside the build target and then execute the build action, IntelliJ IDEA performs the incremental build that compiles only the changed classes. IntelliJ IDEA also recursively builds the classes’ dependencies.

Build a module, or a project

Select a module or a project you want to compile and from the main menu, select Build | Build Project ( Control+F9 ).

IntelliJ IDEA displays the compilation results in the Review compilation and build output.

If you add a module dependency to your primary module and build the module, IntelliJ IDEA builds the dependent module as well and displays it in the output directory alongside the primary one. If the dependent module has its own module dependencies, then IntelliJ IDEA compiles all of them recursively starting with the least dependent module.

Project Structure dialog / Module page

The way the module dependencies are ordered may be very important for the compilation to succeed. If any two JAR files contain classes with the same name, the IntelliJ IDEA compiler will use the classes from the first JAR file it locates in the classpath.

For more information, see Module dependencies.

Rebuild

When you execute a rebuild command, IntelliJ IDEA cleans out the entire output directory, deletes the build caches and builds a project, or a module from scratch. It might be helpful when the classpath entries have changed. For example, SDKs or libraries that the project uses are added, removed or altered.

Rebuild a module, or a project

From the main menu, select Build | Rebuild Project for the entire project or Build | Rebuild ‘module name’ for the module rebuild.

IntelliJ IDEA displays the build results in the Review compilation and build output.

When the Rebuild Project action is delegated to Gradle or Maven, IntelliJ IDEA doesn’t include the clean task/goal when rebuilding a project. If you need, you can execute the clean command before the rebuild using the Execute Before Rebuild option in the Gradle or Maven tool window.

Background compilation (auto-build)

You can configure IntelliJ IDEA to build your project automatically, every time you make changes to it. The results of the background compilation are displayed in the Problems tool window.

Configure the background compilation

Press Control+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Compiler .

On the Compiler page, select Build project automatically .

Now when you make changes in the class files, IntelliJ IDEA automatically performs the incremental build of the project.

The automatic build also gets triggered when you save the file ( Control+S ) or when you have the Save files automatically if application is idle for N sec. option selected in the System settings dialog.

Enabling the Build project automatically option also enables Build project in Settings | Tools | Actions on Save

When you have the Power Save Mode option ( File | Power Save Mode ) enabled in your project, the auto-build action is disabled, and you need to manually run the build ( Control+F9 ).

Compile before running

By default, when you run an application, IntelliJ IDEA compiles the module in which the classes you are trying to run are located.

If you want to change that behavior, you can do so in the Run/Debug Configurations dialog.

Configure a run/debug configuration

From the main menu, select Run | Edit Configurations .

In the dialog that opens, create a new or open an existing run configuration.

Click the Modify options link.

In the Add Run Options list, under the Before Launch section, select Add before launch task . The list of tasks becomes available and the Build will be selected. Click to disable it.

If you need to add a new configuration action, click and from the list that opens, select the desired option.

For example, if you select Build Project then IntelliJ IDEA will build the whole project before the run. In this case, the dependencies that were not included in the build with the Build action, will be accounted for. If you select the Build, no error check option, IntelliJ IDEA will run the application even if there are errors in the compilation results.

Review compilation and build output

IntelliJ IDEA reports compilation and building results in the Build tool window, which displays messages about errors and warnings as well as successful steps of compilation.

IntelliJ IDEA build output

If you configured an auto-build, then IntelliJ IDEA uses the Problems tool window for messages. The window is available even if the build was executed successfully. To open it, click Auto-build on the status bar.

Problems tool window

Double-click a message to jump to the problem in the source code. If you need to adjust the compiler settings, click .

Package an application into a JAR

When the code is compiled and ready, you can package your application in a Java archive (JAR) to share it with other developers. A built Java archive is called an artifact .

Create an artifact configuration for the JAR

From the main menu, select File | Project Structure Control+Alt+Shift+S and click Artifacts .

Click , point to JAR , and select From modules with dependencies .

To the right of the Main Class field, click and select the main class in the dialog that opens (for example, HelloWorld (com.example.helloworld) ).

IntelliJ IDEA creates the artifact configuration and shows its settings in the right-hand part of the Project Structure dialog.

Apply the changes and close the dialog.

Build the JAR artifact

From the main menu, select Build | Build Artifacts .

Point to the created .jar ( HelloWorld:jar ) and select Build .

If you now look at the out/artifacts folder, you’ll find your .jar file there.

When you’re building a project, resources stored in the Resources root are copied into the compilation output folder by default. If necessary, you can specify another directory within the output folder to place resources.

Run a packaged JAR

To run a Java application packaged in a JAR, IntelliJ IDEA allows you to create a dedicated run configuration.

If you have a Gradle project, use Gradle to create and run the JAR file.

For Maven projects, you can use IntelliJ IDEA to run the JAR file. If you have a Spring Boot Maven project, refer to the Spring section.

Create a run configuration

Press Control+Shift+A , find and run the Edit Configurations action.

In the Run/Debug Configurations dialog, click and select JAR Application .

Add a name for the new configuration.

In the Path to JAR field, click and specify the path to the JAR file on your computer.

Under Before launch , click , select Build Artifacts in the dialog that opens.

Doing this means that the JAR is built automatically every time you execute the run configuration.

Run configurations allow you to define how you want to run your application, with which arguments and options. You can have multiple run configurations for the same application, each with its own settings.

Execute the run configuration

On the toolbar, select the created configuration and click to the right of the run configuration selector. Alternatively, press Shift+F10 if you prefer shortcuts.

As before, the Run tool window opens and shows you the application output.

If the process has exited successfully, then the application is packaged correctly.

IntelliJ IDEA — Decompiling/Editing/Recompiling JAR

I realize this may seem like a completely stupid question and this question is a "wall of text", but I’m feeling like I’ve gotten way out of my depth here and need some guidance. I’ve read several SO questions on this topic, but still can’t seem to find one that addresses this particular situation. (please reference update(s) at the end of this post)

BACKGROUND

Our company has an application that was built in Java and released as an executable JAR package by a developer who passed away a couple of years ago. Since then, there has been minimal need for even looking at that code, let alone making any changes. That’s been really good because I do my programming in VB.NET (Visual Studio) and, while I can read and make sense of Java code, I’m not proficient in actually writing/debugging that code.

However, a recent issue has come up that forced me to try to take a look at this Java code due to some internal changes in organization and data structure. I’ve looked in his " src\ " directory and found older versions of his original code but wasn’t able to find the most recent version anywhere. I found some code that made it possible for me to extract the JAR that’s currently being used to a local directory on CodeProject (JarsVB), so I’ve been able to look over some of the .java files when trying to figure out what query is being used for some random operation. I don’t know what tool(s) the original developer used to create the project/JAR, so I’ve installed the IntelliJ IDEA Community Edition 2018 as an IDE, and it’s worked for me so far to simply view the code so I can understand a bit about what it’s doing.

PROBLEM/GOALS

Unfortunately, now there is a need for me to make a change to the Java code and update the JAR, and this is where I can’t seem to make heads or tails of what I need to do. I have my local copy of the code from the "unzipped" JAR containing all the .java and .class files (along with all the other resources), but I’m not sure how to go from that to modifying the code and recompiling the executable JAR.

So, my goals here are as follows:

  1. (properly) Decompile the existing executable JAR. (If the JarsVB solution I mentioned above did what it was supposed to do, I should already have this part handled, but if there’s a better, more "integrated" way of doing it, I’d be open to that as well.)
  2. Modify one or more .java files. (I believe I can figure out the syntax well enough to get this part done.)
  3. Debug/test my changes.
  4. Recompile the changes into an updated executable JAR for release. (THIS is where I’m experiencing the most confusion.)

WHAT I’VE DONE

I’ve been looking at the documentation for IntelliJ to try to figure out how to make this all happen, but I feel like I’m missing stuff. I set my "Project Structure" settings to point to a specific folder, but I can’t seem to get it to actually build anything in my specified path.

Project Structure -> Project Settings -> Project

Project Structure -> Project Settings -> Modules

I went into one of the .java files and made/saved a small change to test, then went to the Build menu and tried all the building options available to me: "Build Project", "Build Module", and "Rebuild Project". Each time, the event log shows that "All files are up-to-date" (even though I changed one), so I go to my output directory to see what it built, but the folder is empty.

I looked at the IntelliJ documentation for Packaging a Module into a JAR File, which says to

On the main menu, choose Build | Build Artifact

But that option is disabled in my Build menu, and when I look in the Project Structure dialog, the Artifacts section is empty, so I try to add a new setting (Add -> JAR -> From modules with dependencies. ), but I’m not sure how to properly set that up either.

Add -> JAR -> From modules with dependencies.

I tried to select a Main Class from my classes/ directory, but it wouldn’t actually accept any of the .class files I selected, so I just left it blank. Then, I did a search for a MANIFEST file, but couldn’t find one anywhere so I left that blank as well. The Build menu option is enabled now, but when I tried to Build Artifact, again, I get nothing in my output directory. In fact, the only files I can find that have changed are in my local working directory. I’m just dumbfounded at this point.

FINAL THOUGHTS/QUESTIONS

I’ve tried to provide as much detail here as I could think of about all the things I’ve tried and gone through to get this JAR updated, but if there’s a question about anything, please let me know. I’m not looking for a "handout" and I don’t expect anyone to do this for me, but I’m also not wanting to become a Java developer just for the sake of making some minor changes to an application that will eventually be replaced by a .NET application. I simply am not familiar enough with the tools or Java development in general to know how to get to where I want to be from where I am.

My decompiled source files are in a totally separate directory from the original, production JAR file because, when I recompile this time, I want to completely recreate the JAR. Am I understanding the Java development process correctly in editing one of the .java files I got from decompiling with the JarsVB and then recompiling the JAR?

Thanks in advance for any assistance in finding the resources I need to accomplish these goals.

EDIT/UPDATE

So, looking at the link in the accepted answer in another SO question, How to build jars from IntelliJ properly?, I figured out at least one part of my issue: Leaving the Main Class setting of my Artifacts configuration blank was a problem. Since I hadn’t been able to select a .class file and I wasn’t sure how to correctly populate that field, I hadn’t given the IDE enough information to operate correctly. I entered the namespace and class ( namespace.class ) I found in the class file that apparently defines the main method for the application, then set the path for the MANIFISET.MF file to my output directory and tried again to Build Artifact.

This time, at least, it DID produce a JAR in my defined output directory, but it’s only 1KB in size. As stated above, the source files are in a completely separate directory from the original JAR file from which they were decompiled. The output directory, too, is completely separate from the location of the original JAR file. Do I need a copy of the original JAR file in the output path for recompiling to work correctly?

I’m making progress, but I’m sure I’m overlooking something "stupid" at this point that’s primarily due to my unfamiliarity with the IDE and developing Java applications in general.

UPDATE 2

Looking at another SO question — how to create jar of java package in intelliJ idea — I learned that I have to individually add the necessary files for repackaging into the JAR. However, this brings up the question, what files do I add? The linked question identifies the .class files, but when I go look at the .class files in my working directory, none of those have been updated recently, so it looks like I’m still missing a step somewhere.

Another question this brings up is, are there certain conventions for Java development of which I need to be aware when preparing my environment? I have my output path set to a completely separate folder than any working or production code, so I’m wondering if something in that setup might potentially cause issues.

As I said before, I made a small change to one of the .java files, then tried both the Build Module and Rebuild Project options, but those are still telling me that "All files are up-to-date". Even so, I tried adding just the .class files from under my classes\ directory to my Artifact configuration and tried again to Build Artifact. I got a bit larger file (approx. 5MB), but when I try to execute the JAR, it just doesn’t appear to do anything, let alone actually launch the application.

I tried again by adding the root folder of my local copy of the source adding everything the root folder contains. (yes, the directory probably needs some "spring cleaning", but that’s for another day)

All files/folders added to Artifact configuration

This time, I got a much larger file this time (approx. 21MB), so I thought I might have fixed the problem. Unfortunately, no such luck. The JAR still doesn’t appear to execute.

For reference, the original JAR file from which the code was decompiled is approx. 59MB in size so, either IntelliJ is doing an incredible job with compression, or there’s yet another step I haven’t yet found. I’m sure this is all a matter of getting my IDE configured correctly, but I just can’t seem to find the right combination of settings.

How to decompile to java files intellij idea?

Decompiling Java code refers to the process of converting compiled Java code back into its original source code. This can be useful in cases where the source code is lost or unavailable, and you need to understand or make changes to the code. The IntelliJ IDEA is an integrated development environment (IDE) for Java development, and it provides a built-in decompiler to decompile compiled Java classes. In this article, we will go over the steps to decompile Java files using IntelliJ IDEA.

Method 1: Decompiling a Single Class File

To decompile a single class file in IntelliJ IDEA, follow these steps:

  1. Open IntelliJ IDEA and create a new project.
  2. Right-click on the project in the Project view and select New → Directory. Name the directory "lib".
  3. Copy the .class file you want to decompile into the lib directory.
  4. Right-click on the .class file in the Project view and select "Decompile".
  5. The decompiled .java file will be created in the same directory as the original .class file.

Here is an example code snippet that demonstrates how to decompile a single class file in IntelliJ IDEA:

This method is very useful when you need to quickly decompile a single class file in IntelliJ IDEA. However, if you need to decompile multiple class files, it's better to use a decompiler tool like JD-GUI or Procyon.

Method 2: Decompiling a Package of Class Files

To decompile a package of class files in IntelliJ IDEA, follow these steps:

Open IntelliJ IDEA and create a new project.

Right-click on the project and select "New" -> "Module".

Select "Java" as the module type and click "Next".

Give the module a name and select a location for the module.

In the "Content Root" section, click on the "+" button and select the directory where the class files are located.

Click "Finish" to create the module.

Right-click on the module and select "New" -> "Java Class".

Name the class and click "OK".

In the "Project" view, expand the module and select the directory where the class files are located.

Right-click on the directory and select "Decompile".

The decompiled Java files will be created in a new directory called "decompiled" in the same directory as the class files.

Here is an example of how to decompile a package of class files in IntelliJ IDEA using the above steps:

In this example, we create a new project and module, add a directory where the class files are located to the module, and then decompile the class files using the Fernflower decompiler. The decompiled Java files are saved to a new directory called "decompiled".

Method 3: Decompiling External Libraries

  1. Open IntelliJ IDEA and create a new project or open an existing one.
  2. Click on "File" in the top menu bar and select "Project Structure".
  3. In the Project Structure window, select "Modules" on the left-hand side.
  4. Select the module you want to decompile and click on the "Dependencies" tab.
  5. Select the library you want to decompile and click on the "-" button to remove it from the module.
  6. Click on the "+" button to add the library back to the module, but this time select "Attach files or directories. ".
  7. Browse to the location of the JAR file for the library you want to decompile and select it.
  8. Click on "OK" to add the JAR file to the module.
  9. Right-click on the JAR file in the "External Libraries" section of the project window and select "Library Properties".
  10. In the Library Properties window, click on the "+" button next to "Sources" to add a new source.
  11. Browse to the location where you want to save the decompiled Java files and select it.
  12. Click on "OK" to save the changes and close the Library Properties window.
  13. Right-click on the JAR file again and select "Download Sources".
  14. IntelliJ IDEA will now download the sources for the library and save them to the location you specified in step 11.
  15. You can now view and edit the decompiled Java files in IntelliJ IDEA.

In the above example, we create an ArrayList of Strings and add two strings to it. We then loop through the ArrayList and print out each string. This code is written in Java and can be decompiled using the steps outlined above.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *