I know how to run my Java program from the command line. My path to Main.java is:
my_dir/turtlesolution
turtlesolution has the following:
Main.java
Turtle.java
Turtles.java
I opened turtlesolution as the project, using IntelliJ. I then opened (by double clicking) Main.java. I want to run this. I have to edit configurations before I run it, it seems.
So I clicked edit configurations. I clicked JAR application because this looks like the option. This is what comes up:
Working directory, I set it to point to turtlesolution. What is Path to JAR? I tried to set it to turtlesolution but it remains blank. What does Warning, jar file doesn't exist mean?
I can run it from the command line by just doing
cd turtlesolution
javac Main.java Turtle.java Turtles.java
cd ../
java turtlesolution/Main
Aapprently it's very easy to figure out. I'm in my final year of learning CS in university and apparently it's simple, but I can't figure out how to do it, and it's too simple to teach ...
If you don't have any run configuration in the project, Run button is also disabled.
If you right click in a file with main method, run configuration is created automatically and main class value if prefilled. This worked for me for more vist https://intellij-support.jetbrains.com/hc/en-us/community/posts/206811415-Problem-with-Edit-Configurations-
Related
I've a Java backend application (Traccar). I compile and start the application selecting the following configuration:
The I click on run to execute the program, and everything works. My question is: how can I do the same from command line? All the settings in this image, how are they translatable to some commands?
If I have to do run the program from command line, how can I select the main class (org.traccar.Main), the module (-cp tracker-server.main) and so on?
Thank you everybody.
You should compile the JAR file first and then run it using something like this:
java -jar tracker-server.jar conf/traccar.xml
I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version
I'm currently in the process of learning more about Java. I downloaded a sample program that has about 5 different .java files in the source folder.
Whenever I try to execute the class files in the command prompt using "java ClassNameHere", I get a message in the cmd prompt that says "Error: Could not find or load main class ClassNameHere".
I've tried recompiling them using the javac command and they compiles without any issues. Its just the class file i'm having trouble with.
In addition to this, I also cannot run the java files using Eclipse. Instead of getting an option to "Run as Java Application", "non applicable" appears under the "Run as" tab in Eclipse.
I've tried a number of different solutions already posted online, including another command that uses "java -cp . ClassNameHere" or something along those lines and they have not worked for me. Any help is appreciated.
Edit
Link to program from my drive since I'm not home
https://docs.google.com/file/d/0B9_pcTVZTnfEeVdzZmM0ZmFkTmc/edit?usp=docslist_api
Here is a a procedure that will get the application working in Eclipse. This procedure assumes the following:
Windows 8. I don't think Linux/Mac OS X procedure would differ greatly, but just in case...
In Eclipse:
Right click in Package Explorer, and select New --> Java Project.
In the New Java Project dialog, uncheck Use default location, and click Browse and select the directory you've extracted the zip to, or type the path in the box.
Click Finish.
From this point, you can run this is a Java Application from Eclipse.
Confirm that at least one of the classes has a main() method. That would be required for Eclipse to show "Run as Java Application".
Follow this steps:
Run Command Prompt.
Use cd C:\path\to\your\porject\java classes.
Use set path=%path%;C:\Program Files\Java\jdk1.7.0_65\bin (CHANGE THE PATH TO YOUR jdk1.x.x_xx\bin FOLDER) to tell the system where to find jdk programs.
Use javac MainClass.java to compile the project, MainClass.java si the java class where the main method is.
Use java MainClass (WITHOUT .java EXTENSION) to run the project.
After the last command your project should run correctly.
Sorry for the "use" ripetition but i'm not english.
So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?
The things that seem to be needed in NetBeans are:
The project has to be the Main Project (by right clicking on it in the Projects list).
You have to set the Main Class in the project properties. (Right click, Properties, Run, Main Class.)
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist subdirectory.
If that fails to fix the problem, here's a longer story...
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
java -jar filename.jar
(When you say it works for you from the command line, is this what you're typing?)
At that point, the Java executable looks for a file inside the jar named META-INF/MANIFEST.MF. And then in the contents of that file, it looks for the value of a property, Main-Class. And finally it looks for the class of that name in your jar and runs its static main(String[]) method.
So if your jar is failing to run, you can do the following to debug what's going on:
Clean and rebuild your project in NetBeans.
Double check that your class(es) are actually in the jar:
Start a command prompt
cd into the dist subdirectory of your project.
Use a command like jar tf filename.jar to list what's in there.
Double check that the MANIFEST.MF file is correct:
Again in a command prompt
cd into the dist directory.
Use a command like jar xf filename.jar META-INF/MANIFEST.MF to extract the manifest.
Look at the contents of that file (e.g. type META-INF\MANIFEST.MF) and make sure Main-Class is set to the appropriate class.
If all of the above check out, then double clicking the file should work.
Have you set the containing project as the "Main Project"?
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.