For a class exercise on game trees, I have to write code that works with a .jar file. I haven't used Java in this way before. My Eclipse project tree looks like this:
To run the code, I was told to do this on the command line:
java -jar VierOpEenRij.jar Mens spel.speler.Random 5 5
How do I debug this code? I have tried tinkering with Eclipse's debug configurations and I also tried executing jdb -jar VierOpEenRij.jar Mens spel.speler.Random 5 5 but I can't figure out a way to start the debugger.
How do I debug my code when a .jar file contains the main method?
I'm not sure about debugging with jdb, but if you're debugging in Eclipse, it won't matter what the final packaging(e.g. jar) is. If you set up the debugging for the Random (class with the main method?) class then you will be debugging your program just fine.
I might be completely off as I haven't really been in this situation, but wouldn't right-click on the jar -> debug as ... -> Java application do the trick?
EDIT:
Managed to make it work this way:
need a "semi" runnable jar (seems to be your case: main class in there but missing dependencies, and the manifest should indicate a main class)
add the main to the buidl path (in package explorer view, right click on jar -> build-path -> add to build path).
in the package explorer view's build path node, expand the jar until your drill down to the main method, right click and run or debug as java application.
For some reason I would expect to be able to right-click the jar and do directly dbug as java app though... the main is indicated in the manifest, but it doesn't seem to find it. Don't know why. But in the meantime, that works.
EDIT2: Actually, now I can directly right-click the jar (or even the project) and select the right main when a dialog pops up, and it's all good. Maybe I missed something earlier. Any way, you're good to go.)
In the picture below, MainInJar.main() calls ClassOutOfJar.somethingOutOfJar(), which prints something to the screen.
You need to specify classpath in jdb.
jdb -classpath [jar-file] [main-class-full-name] [args]
In your case the jar file will be: VierOpEenRij.jar
Main class name you could find in jar archive in META-INF/MANIFEST.MF.
Related
This is my first question on stack overflow.
Please have a look at this first, please!!
I am creating an executable jar file by clicking on the button surrounded with grey color. The problem is that the exxcution of the application always starts from the "PersonalAssistant.java".
I want my application to begin its execution from "App.java" which also contains a main() method.
So, basically the jar file which I want should begin its execution from App.java class which has a main method.
I don't know how to change the path or execution class for jar.
Please help me!!!! I'm stuck with this silly problem. But I want a solution for this.
I'm using Windows10 with Netbeans 8.2 IDE
You should be able to reconfigure your IDE and rebuild the executable jar specifying which class you want to use as the executable.
in your IDE add java -cp myjar.jar App
to your compile command
this might also help
Look at the picture above. Right-click on your project and select Properties. Select Run. Look for the Application Class field. In my example, I have javafxapplication211.Main. This structure is projectName.MainClass. You should just need to change the MainClass part to the class you want to start your project. For example, if Main.java was starting my app and I wanted to change it to App.java. I would change javafxapplication211.Main to javafxapplication211.App.
If you hit Browse, it will give you a list of available classes that can start the application.
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.
I've got a project in which I'm using Java+Scala+Slick2D.
The project itself runs well when launched from within eclipse. But when I try to make a jar file, it just refuses to work. Here's the error I keep getting when trying to export it as a Runnable jar:
And if I try to export into just a Jar file, it's unable to find the Main Class:
There is, of course, a main class in game.TicTacGame. But it refuses to acknowledge it. I tried creating an executable with a simple Hello World project and it worked fine. It even detects the main class. Why is Eclipse not detecting the main class in this case?
PS: I've also tried extracting the .jar file created, editing the manifest.mf file to add the Main-Class: game.TicTacGame, enter two new lines and recreate the jar. Then it gives me a corrupted jar file error.
I'm at my wits end and would appreciate any help in this regard. I'm using Windows 7 x64 with Eclipse Juno, Java 1.7 and Scala 2.10
Edit: The Main class is in Java
Okay, I got it to work. Apparently, all I needed to do was restart eclipse. And then magically, it started detecting the Main class:
But the jar started giving me noClassDefFound errors for the Slick2d, LWJGL and other libraries. That's where JarSplice came to my rescue. I exported the project as before, with all the libraries and resources.
Then, I fired up JarSplice and added everything as follows:
Go to "Add Jars" and add the project.jar just created through eclipse, add lwjgl.jar, slick.jar and scala-library.jar. lwjgl and slick should be in your project lib folder where you would have imported them. scala-library should be available wherever it says it is. Make sure you add that as well
Next, when I tried to "Add Natives" it kept giving me "Duplicate library" error. So I removed them all and kept it empty.
Next in "Main Class", I entered the path to the main class i.e. game.TicTacGame
Finally, "Create Fat Jar". And it works perfectly :)
I just encountered the same problem, and here is how I solved it:
Open "Run As" --> "Run Configuration" on the project you want to export
Click "Search" for Eclipse to refresh the list of main class
Then export Runnable JAR file again, and everything goes smoothly.
The most easiest method is run the java file once and automatically the file appears in the list.Even i was facing the error but it was solved by using this simple method.
I recently figured a better way to do this using 'Runnable jar export' which might help you. In order for your main method to be listed in that list, you need to add the main method to the Run Configuration list.
This way it's simpler to create a runnable jar especially if you want to do it repeatedly.
Well, got the same Problem and solved it by selecting my GUI to export and not the whole Project.
I had this problem with Eclipse version 2019-03 (4.11.0) and compiler JaveSE-11. Choosing the right launch configuration and exporting runnable jar failed over and over again with an error
Could not find main method from given launch configuration
I've tried to restart Eclipse and do a clean build, but it didn't help.
In the end, I found a workaround to go into the generated jar file (I've used 7zip) and change META-INF/MANIFEST.MF file. The file should have something like this inside:
Manifest-Version: 1.0
Class-Path: .
Main-Class: <package name>.<class name>
Hope it helps someone.
I faced the same issue and in my case I found that "launch configuration" was incorrect in Runnable Jar file specification dialog. Somehow, eclipse was automatically taking it. The "Launch configuration" should be java file which has main method. Basically, it is filename-package.
The moment I changed my "launch configuration", I was able to create Jar file without any error.
I had the same issue. To overcome this issue, you need to close all opened files and open a single java class file that has the main() method. Then export it from eclipse and works fine.
I want to create a runnable JAR that include referenced libraries, namely jackson, in eclipse via the export. There used to be fat jar that seemed to be the goto solution before Eclipse offered to export to runnable JAR.
From this question, the first option would be what I'm looking for. Since it is for the moment a small project, it wouldn't be a problem to always have everything together (plus it's easier for me since I'm just starting with JARs, runnables and jnlp)
When I try to launch it, there's an alert window saying there was a problem and to try to find it in the console (which I can't get to open).
If I remove any use of this referenced library everything runs perfectly (except the part that it's not doing what I want at all). The window looks the way I designed it, but no functionality without the library.
I don't even have a stack trace to help with the problem. I've created other projects without any referenced libraries and everything is fine. So I've pretty much narrowed it down.
Since I let Eclipse handle the exporting and everything, I don't suppose you need the manifest to see if it is alright. Any questions you might have, I'll provide the informations you need. It goes without saying that the project runs directly from Eclipse.
Eclipse
Version: Indigo Service Release 2
Build id: 20120216-1857
Edit following Baqueta's answer
What happens when I do that is that I get a custom RuntimeException. I'm a bit baffled here. Here's a simple explanation of my code.
I have a utility class (UtilityJSON) that uses the referenced library (jackson). The constructor of that utility class instantiate an object defined in the referenced library, let's call it mapper.
A method of UtilityJSON uses a method of 'mapper'. At this point the exception is raised. Why I'm baffled is that 'mapper' is succesfully instantiated but using its method raises an exception. If the jar couldn't find the referenced library, it probably couldn't create the object 'mapper'. But running the project from Eclipse is all fine.
Is there any way to debug when running an executable jar? Maybe echo something in the console.
Edit 2
I may have found the error. My program reads a *.txt file in a folder inside the project. When exploring the content of the jar I see that this folder was not included, thus the exception. So the question now becomes: how do I include this folder? It's in the root folder of the project.
You could try the following:
In Eclipse, go to Project->Properties.
Select 'Java Build Path' from the column on the left.
Select the 'Order and Export' tab.
Find 'jackson' in the list and make sure it's checked.
EDIT
To answer your updated question:
In Eclipse, find the *.txt file in the Package Explorer. Right-click it and select Build Path->Add to Build Path. Then follow the instructions above to make sure the file gets included in the Jar. Finally, do a clean and rebuild.
If you're ever going to add more resources, it is common practice (and makes sense!) to have a resources folder (often called 'res'). You can then add the entire folder to the build path, so that all the resources in there get included in the Jar.
Enable the java console by writing
javaws -viewer
in a terminal. Close the Cache Viewer and click the advanced tab. There open Java-Console and click "enable console".
Can anyone help me with this? I have not been able to find anything that answers this for exactly what I need. All the answers I find have to do with adding additional libraries to manifest files and what not.
Here's the situation:
I have written a game using NetBeans 6.9. The game is in Java. There are about 80 classes. All classes are contained in the default package. The game executes correctly. I've been working on this project for about 18 months and I always maintain executable code as I have developed the game.
What I did:
I needed to import something from a static class I'd written, into another class I was working on. In order to do that, I had to move everything out of the default package. I used NetBeans to refactor everything into a custom package.
What happened:
When I do a "clean and build", the project builds successfully.
When I do a "run main project", I get the following error:
java.lang.NoClassDefFoundError: WarMachine
Caused by: java.lang.ClassNotFoundException: WarMachine
...stack trace
Could not find the main class: WarMachine. Program will exit.
I have checked the .jar file and I see that all the compiled .class files are there, including the main class (called WarMachine.class). All the .class files are in a directory called Machine (that was the package name I had NetBeans refactor everything into).
From what I have been able to find on the internet, the problem is that my manifest.mf file does not point to the correct location of the WarMachine.class file.
What does my manifest file need to say? I don't use any other libraries or anything like that. I have 78 classes, all of which are in the "Machine" package. Can anyone help me?
Thanks for your time!
You have to tell Netbeans where the main class file is. Right click on your project, select "Properties", then go to the "Run" tree element on the left.
You will now have a "Main Class" textbox on the right. Click "Browse" and select your main class. Netbeans should then fix the manifest file for you.
In case you are curious, your manifest file inside the .jar file of the dist folder should have a line like this:
Main-Class: Machine.WarMachine
As an aside, it is considered standard practice to use lowercase letters for package names. You should call your package machine.