1

This must be a very simple task for many of you. Let me explain the scenario.

I recently started practicing questions on HackerRank. But, I found the platform not so friendly for debugging. No online platform is or can be because of its own limitations. HackerRank provides question and stub code for many of the problems on its problem page.

For example, let us consider, https://www.hackerrank.com/challenges/java-datatypes/problem

But, because of it's debugging limitations I can't make the best use of portal. Hence, I wrote a PHP script to scrape all the content from the website and generated problem statements in HTML/PDF formats and solutions in java format.

Here's the GitHub project for the same.

https://github.com/saint1729/hr-idea-integration

The main intention of this activity is to have an integration of the website with an IDE like Intellij IDEA. This is now complete.

I created a gradle project with existing sources. But, the project contains many java files (almost 500+ files and each file has it's own main method). My intention is to solve one problem at a time and see if it compiles and submit it using a REST API provided by HackerRank.

But, when I am trying to Right Click and Click on Solution.main() for every file, it tries to compile all files in the project and because there are some compilation issues with the project, I am unable to test my code for the current file. This is not productive for me.

Please let me know if it's possible to compile and run a single file in IDEA (without compiling the whole project). If the idea of creating a gradle project for this activity is not necessary, can somebody recommend me another efficient solution?

NOTE: Every scraped java file contains it's own main method. I know that a project can contain only 1 main method. But, I don't know a coherent solution to solve my problem.

Sai Nikhil
  • 1,237
  • 2
  • 15
  • 39
  • "I know that a project can contain only 1 main method" .... who told you that? – Stultuske Mar 24 '20 at 08:51
  • I might be wrong. Please correct me if I am wrong. I am not an expert in handling projects and dependencies. – Sai Nikhil Mar 24 '20 at 08:53
  • every class can have it's main method. when taking overloading in account, every class can have an unlimited amount of main methods. The limitation is that only one method at the time can be used as entry point. but what is stopping that one from calling other (main) methods? – Stultuske Mar 24 '20 at 08:54
  • The problem I am facing here is that since I have so many files and some files contain compilation errors, it is showing compilation issues of other files when trying to run the main method in the current file. I don't want that to happen. Let me know if any further explanation is required. – Sai Nikhil Mar 24 '20 at 08:57
  • See https://stackoverflow.com/a/20677403/2000323 about how you can run the code even with errors. Since you are using Gradle - you will need to disable Gradle build/run actions in IDE. For this switch the Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | **Build and run using** option to **IntelliJ IDEA**. – Andrey Mar 24 '20 at 09:00
  • Hi, I tried this. It is still trying to compile other files in the project. I don't want this. My aim is quite simple. Write in a file, compile and run that file. See the output. Do not care about other files in the project. Is this possible? – Sai Nikhil Mar 24 '20 at 09:26
  • The problem now seems to be with many files in a single project with the same filename. It is getting tougher and tougher setting up my project. – Sai Nikhil Mar 24 '20 at 10:19

1 Answers1

1

If you want to continue using gradle, you create a module per solution.

Let's suppose you have 3 solutions. canyouaccess, duplicateword and java1darray.

So your repository looks like this:

java
   canyouaccess
      src/main/java
          package
             Solution.java
   duplicateword
      src/main/java
          package
             Solution.java
   java1darray
      src/main/java
          package
             Solution.java
build.gradle
settings.gradle

Each module can have its own main. Inside a settings.gradle file the modules can be defined or disabled by commenting it out.

Your build.gradle looks like this:

...
subprojects { project -> 
    apply plugin: "java"
    sourceCompatibility = 11
}
...

For the settings.gradle looks like this:

include 'java:canyouaccees'
include 'java:dublicateword'
include 'java:java1darray'

Each module can be build separately, you could even group modules by creating a sub module structure.

And each module can have it's own debug configuration, where the module and the main is selected. If your set them as shared, they are stored in xml format under .idea/runConfigurations. So your script can create them as well.

Each module needs it's own gradle.build file, where the main class is declared.

jar {
   manifest {
     attributes('Main-Class': 'your.main.class.goes.here')
   }
}

Something like this should do.

Chris
  • 5,109
  • 3
  • 19
  • 40
  • There is no compulsion to use Gradle. Can you suggest me if there's a better way? Also, can you provide a link to the article to set up my project to function as expected? – Sai Nikhil Mar 27 '20 at 17:44
  • From my point of view, a build tool like Gradle is the best choice. I will provide an example tomorrow. What info do you need exactly? – Chris Mar 27 '20 at 18:33
  • I hope you can access the files in the following repository. Every directory inside the directory "java" has a problem directory and inside that, there is a "Solution.java" file. I want to efficiently create a project out of these files with minimal manual intervention. Later, I will modify each "Solution.java" file independently and after solving the problem I will submit it on HackerRank. Is this possible? – Sai Nikhil Mar 27 '20 at 19:16
  • It would be of great help if you can provide me an article/video that explains me how to solve my problem. – Sai Nikhil Mar 27 '20 at 19:18
  • You didn't Post a link to your Repository. I don't think there is an article/video for this specific problem. I can show you how to set up a multi module gradle project, adapting it will be your job. – Chris Mar 27 '20 at 20:13
  • Sincere apologies. Here it is. https://github.com/saint1729/hackerrank – Sai Nikhil Mar 27 '20 at 20:59
  • Sure. That would be of great help Thanks – Sai Nikhil Mar 27 '20 at 21:00
  • I just updated my answer. Sorry for the delay, stackoverflow was in maintenance mode. Hope everything works, I don't have the possibility to properly test it at the moment. Please let me know if you need further assistance. – Chris Mar 28 '20 at 16:28
  • The structure looks good. Can you help me set up the project? Thanks. – Sai Nikhil Mar 29 '20 at 20:16
  • What do you mean by that? – Chris Mar 30 '20 at 05:19
  • This is the first time I'm creating a gradle project on my own. Anyways, I tried on my own and almost succeeded. But, while trying to run my programs, I identified that I'm unable to take user input through System.in. I checked on this in other posts and they suggested to add run method in build.gradle. But, that didn't work. It says that run method not found and unfortunately I'm not not able to even compile my project. I'm wondering how people work with all these difficulties with build tools. – Sai Nikhil Mar 30 '20 at 06:36
  • Yes, build tools are a bit annoying sometimes :) ok, I can help you. But it might have to wait till next weekend. – Chris Mar 30 '20 at 07:05
  • Cool. I got it working finally. I added the "run" method in the module's build.gradle instead of root project and it works as expected. I also need to add the "mainClassName" and only then run makes sense I think. – Sai Nikhil Mar 30 '20 at 08:35
  • Great, glad to hear that :) Could you please accept the answer then? – Chris Mar 30 '20 at 08:53
  • Done. Awarded the bounty as well :) – Sai Nikhil Mar 30 '20 at 09:05