简体   繁体   中英

How to save Java Project from IntelliJ IDEA to Git repo so it will build on another PC after cloning?

I'm building a simple calculator app from a tutorial/class on Pluralsight (Java Fundamentals class), which starts with a Main.java file, and adds a class file later, MathEquation.java .

I created a Git repo to track my progress, and uploaded to Github: JavaFundamentalsClass . I used GitIgnore.IO to find a Java IntelliJ .gitignore file , and everything seemed to be working fine on the original PC I was using.

However, after switching over to a different PC, with a fresh install of IDEA, I cloned down the repo from Github, and found that the project would not build and/or run. I get this error:

"C:\\Program Files\\RedHat\\java-1.8.0-openjdk-1.8.0.191-1\\bin\\java.exe" -Dfile.encoding=windows-1252 -jar ""

Error: Unable to access jarfile

Process finished with exit code 1

I suspect that there is something in the .gitigore file that is keeping some of the project, dependency, or build information from being saved in the repo that keeps IDEA from having all the info it needs to build and run the Main.java correctly.

Can anyone help? You should be able to download the project from the repo linked above in it's currently broken state, including the .gitignore file.


Additional Info

I wiped my repo clean and recloned and I forgot, originally it doesn't even give me the run button (it is grayed out). I had to do a procedure with "Add Configuration" from a search I did for that to show up to get the error above.

Also, I did install java from the OpenJDK, rather than oracle. I figured with Oracle cutting off commercial development maybe start the switch now as I'm learning, but maybe that's the issue (normal Java on my other PC).


It's working, but...

First thing - Intellij needs to know where Java is. It appears that if the SDK/JDK is not installed prior to Intellij, you will need to tell it where Java is after installing. Or if you use OpenJDK instead of Oracle Java SDK. You can do that at View/Open Library Settings , under Platform Settings / SDKs (select the JDK home path ).

Then, after setting up Java, I am able to get everything working in Intellij IDEA if I find the Main.java file, right-click, and choose "Run main.main()". This runs the program okay, and also creates a Main configuration in the Run/Debug configuration area, and finally the "Run" button becomes available.

So I can get it working, but if I wipe all the files and clone it fresh from the remote repo, I have to go through running the Main.java file directly again to recreate the configuration.

So, this still doesn't answer my original question. Why doesn't this configuration get saved in the repo? What file is this information saved in? Is there something in the .gitignore file that is keeping this information from being saved to the repo?

Look like you have a different java version on your second machine.

  • First of all, check your java version
  • Update the configuration of your project, right now it pointing to java 1.8

https://github.com/LightCC/JavaFundamentalsClass/blob/master/.idea/misc.xml

在此输入图像描述

在此输入图像描述

I think it might be because of java not installed on the other PC you were trying. If it is installed just check whether the path mentioned has java executable file.

  1. The first part is ensuring Java is setup, and that Intellij IDEA knows where Java is, per the other answers, comments, and addendums to the question.

  2. The second part is setting up the run/debug configuration, per the "It's working but.." section of the question. This can be created automatically by right-clicking the file that has the main class to be run (usually Main()...), and selecting the "Run Main.main()" option (replacing Main and main() with the file and function which needs to be run)

  3. The final piece is that to get the run/debug configurations to save to the repo, you either need to:

    • Share the workspace.xml file (ie make sure this is not in the .gitignore file). However, there is a lot of user-specific stuff in that file, so it should normally be excluded from the repo.
    • Or, edit the configuration and checkmark the "Share" box in the upper right-hand corner (just right of the "Name" field). This will put the configuration into a separate folder inside the .idea folder, which should not be excluded from the repo by .gitignore .

在此输入图像描述

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM