Is it possible to use a class from a .class file in IDEA? For example, if I have a directory with two files Ball.java and DemoBall.class, how would I use DemoBall in Ball? It says it cannot find symbol when compiling. I just want to tell IDEA that there is a compiled Java class in the directory and to recognize that, I don't want to decompile it.
EDIT:
Open IntelliJIDEA. Now Press CTL + Shift + ALT + S. This will open Project Structure.
Now Select Module from Project Settings (left hand side). And Dependencies tab from the right hand side.
Now Click on the + on the extreme right hand side toolbar. Select Jars or Directories. A new Window, Attack File or Directories will pop up.
Now browse to the location, where DemoBall.class is located. Click OK.
Select the CheckBox under Export against the location just selected. Click OK on the main Project Structure window. You are done.
Original(This one it seems is not the right way):
Open IntelliJDEA. Create a Project, with simple Java Class named Ball.java
inside it, having one main method(empty body).
Run this class, so as to create one out folder.
Copy DemoBall.class. Now through Windows Explorer/File System, go to out folder and paste DemoBall.class inside out\production\ProjectName folder.
Now simply write the code that uses this DemoBall class, inside the main method of Ball.java class.
Previously, I was wondering, as to why the red lines are coming still, even though, the program is running fine. But once, I restart, IntelliJIDEA, everythingy works fine, without showing any errors.
Related
I have been at this problem for a few days now, don't know why it is still happening. I have tried other solutions that have been posted on stackoverflow but no luck.
Below the following code is from the book crafting interpreters in java.
In IntelliJ 2019.4 this error always pop up when I am trying to run the .jar file.
Error: Could not find or load main class com.jam.Jam
Caused by: java.lang.ClassNotFoundException: com.jam.Jam
I have built the .jar file and setting the main class as com.jam.Jam and the class path for the .jar file as /home/username/Jam-Programming_Language/src/com/jam/Scanner.Scanner.
This is the layout of my project directory.
The following solutions I have tried:
Solution 1
Open Project Structure
Select Modules, then click on the module for which you want the
dependency
Choose the Dependencies tab
Click the '+' at the bottom of the page and choose the appropriate
way to connect to the library file. If the jar file is already
listed in Libraries, then select 'Library'.
and
Solution 2
Go to the Project Structure, click on Modules, and click on your
Module
Choose the "Dependencies" tab
Click the "+" button on the right-hand side and select "Jars or
directories..."
Add the directory(ies) you want (note you can multi-select) and
click OK
In the dialog that comes up, select "classes" and NOT "jar directory"
Make sure you're using that Module in your run target
It had no effect apparently. Any clue on what I should do?
Edit: Uploaded wrong screenshot, replaced screenshot.
Thanks to CrazyCoder for pointing out the errors in my project (silly mistakes). It seems that after I have resolved those mistakes.
I had to follow the solutions I have posted then rebuild the .jar file.
Afterwards I need to mark the src folder as a source root folder (right click folder -> Mark Directory as -> sources root)
Edit Configurations if pointing to wrong main class.
Set Project compiler output (File -> Project Structure -> Project) to a folder designated to hold the output (any folder basically in or outside the project).
The error says, it is trying to run: com.jam.testfile. Your class is called: com.jam.Jam.
Click the dropdown where it shows "testfile" with a little red X on it. Select "Edit Configuration." For the "Main class", select "com.jam.Jam". That's it!
BTW, the little red X means there is some problem with your configuration.
Related: IntelliJ does not show 'Class' when we right click and select 'New'
In the above accepted answer, it says the "New" does not give the option for Java Class because it was not the source root (blue directory).
However, as you can see, even though where I am right-clicking is source root, indicated by blue box, it is not giving me the option for Java Class
Any idea why?
This directory is imported from git so it did not follow the usual "Create Project" for java directly from Intellij
You are right clicking in the wrong spot -- youre "too high" up the tree. Go to where you code is (as in the package under "src") and try it there and you will see the option for a New Class.
EDIT: You don't need to be in the source root. You need to be in the source directories themselves. The blue dot I believe is a git status indicator. What you want is an entirely blue folder.
Go down under "src". Likely under "src" then "main" then "java", and you're there (that's blue for me) and then you will see your package folders. Below those are where you likely want to be adding classes.
As many mentioned above you can right click on the folder you would like it in. In your case:
If you push Alt+1, it might be easier for you to right click
src > New Java Class
Alternatively you can push:
Alt+1 (Project View)
Alt+Ins
or
Ctrl+Alt+Ins
1) Right click on src
2) Click New > package
3) Name your package and press OK
4) Right click the newly made package
5) New > Java Class
If the folder where we are trying to create the class is not marked as source or test then the option is not available.
Right click the project and go to project structure then under project settings -> Modules, Select the folder where you wanted to create class and mark as Source or Test appropriately and then click apply and ok.
Now you will see the change of color of the folder/package(Sources->Blue, Test->Green) and you will get the option to create the class.
This worked for me.
I know this is an old thread, but it happened to me because my gradle files were not synchronized. So if you are within a team working on same project and face this issue, make sure to run gradle sync.
You should go inside java project then you can create java class.
I have been trying to figure this one out for a bit, not coming up with the right approach. I read through this question, which I'd already tried, but it's not working quite right.. using IntelliJ EAP 142.4675.3
What I am trying to do, is to debug a junit test using a jar (extending the Provider class) file I've placed into JAVA_HOME\jre\lib\ext. I want that jar to be debuggable as well.
The problem is, if I attach my source directory as the source for a jar, then, in a stack trace, when I click the named file (containing a method I wish to view), IntelliJ asks me to choose between two of the same file, presumably because it knows about the source (it's in a module in my project) and it's also been told about the source via the jar source path I added.
I tried adding the provider.jar to the project module dependencies tab, which caused intellij no end of grief. Reverted back to having it in jre\lib\ext, and, in the Platform Settings->SDKs->1.8->Sourcepath tab, I added the module sourcepath. This lets intellij step into the .java file for the jar, vs the .class file, but, when it does so, I now get a banner at the top of my code windows which says "Alternative source available for the class xxx" and there's a combobox at the RHS with the module name listed twice (in the latest EAP, it actually lists the module and the jar [same name as module, with .jar]). And, as mentioned earlier, the IDE asks which which (of the very same) file I wish to edit when I click a file in the stack trace.
Clearly, there is something not quite right.. what am I missing? I find it odd that the ide will not open the source code when stepping into the jar until I attach the code as above, but, when I do, it sees it twice.
So let's start from scratch:
File > New Project, choose "Java Module", fill the form:
Bypass the form asking about the kind of project (webapp etc) by clicking on "finish"
you now have a project with a src dir:
right click the 32910506 directory and choose "New" then "Directory" and create a new directory named lib.
drag and drop your 2 jars (sources and code) to this directory (I will use common-lang for this example), beware to copy, not move:
now right click on src, choose "New", "Java class", name it App:
right click your "code jar" and choose "Add as library":
in App type psvm then hit TAB to get a main and fill it like this:
right click on your "code jar" and choose "Open library settings":
click "+" and choose "Attach file or directory" and select your "sources jar":
Now if your Ctrl+click on random in App, you should end up to the method source:
Put a break point in the random method:
Now right click App, choose "Debug App.main()", you should end up to the previous breakpoint:
Used: idea 12.1.6 under linux
I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.
So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.
I write some code and the main run and debug buttons are greyed out! I can't run my code!
I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!
Edit: I am also unable to run my file regardless of if its in a project or not.
Edit 2: Screenshot of my project setup
Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).
My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:
Right click the folder containing your source
Select Mark Directory as → Test Source Root
Some of the classes in my folder don't have a main() method, but I still see a Run option for those.
right click on the "SRC folder", select "Mark directory as:, select "Resource Root".
Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.
you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,
Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:
public static void main(String[] args) {
}
I had the similar issue and solved it by doing the below step.
Go to "Run" menu and "Edit configuration"
Click on add(+) icon and select Application from the list.
In configuration name your Main class: name of your main class.
Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src
This is where I had issue and after correcting this, I could see the run option for that class.
Something else that worked for me:
Right click the folder in src containing your main
You'll see an option "run 'file.main()'" with the run icon.
Click it, and then the run icon in the top right and bottom left will turn green from then on.
Sometimes, patience is key.
I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.
So I waited for the indexing to finish and only then I was able to run my main class.
If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Click on add icon and select Application from the list.
In configuration name your Main class: as your main class name.
Set working directory to your project directory.
Others: leave them default and click on apply.
Now you can run your program.enter image description here
Last resort option when nothing else seems to work: close and reopen IntelliJ.
My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this
-First Move Your Code Files in side the "src" Folder
-Make sure your Main method is declared like the following
public class Main {
public static void main(String []args){
}
}
then:
Go to Project configurations
select Java application,
check allow parallel run
and select your main class
and it should work
If you are just opened a new java project then create a new folder src/ in the man project location.
Then cut and paste all your package in that folder.
Then Right click on src directory and select option Mark Directory As > Sources Root.
If you use Maven, you must to declare your source and test folder in project directory.
For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.
My eclipse seems to be pretty screwed on my laptop. Whenever I load a program up from College, it does not detect the errors or anything. Also now when I attempt to run the program, it won't allow it; i'm given the message
unable to find an ant file to run
Anyone able to help me out?
1-Create a new project in Eclipse
2-After the project is created, look in the package explorer window pane on the left and right click on the src folder.
3-There are two methods for the next step, you could either add a New > Class, and then copy and paste everything from your old java file to the new class (make sure the class name is the same), or the better route would be to Import.
4-After clicking Import, select File System under the general folder. Click Next.
5-Browse for the java folder where your source files are located. Once you click ok, it will add all of the source files to the right pane.
6-Select which files you want to add and click Finish.
7-Now if you look at your Package Explorer window you should see the source files. Now just
compile them, and the error shouldn't appear and you can run them just like before.
By creating the new project, you're making sure Eclipse knows where your source files are located (in ./src) so that it can compile your code in that location.