Newbie on eclipse, I have a naive questions.
I have found nowhere in Eclipse to execute the equivalent command line "javac". If I understand well, "run" from the menu of Eclipse processes as javac + java. So my question is: Under Eclipse, how can we compile a .java with "javac" only ? Thanks.
For information, I notice this similar post
How to run Javac from Eclipse
does not answer my question. Mine is to "compile a .java file without running anything"
Well, you can make Eclipse run Ant for you... but normally you just get Eclipse to compile the code for you with its built-in compiler. There's no need for javac.
By default, Eclipse will build every time you save - you don't need to explicitly build at all. When you use "Run" in Eclipse, it doesn't need to compile at that point, because the code has already built.
If you want to find the class files, just look under your output directory.
Eclipse should build your projects automatically so you don't need to call javac
Related
I'm a beginner programmer working on my checkers game and I happen to have a problem which seems impossible to overcome. It's probably something obvious and stupid but I've searched for days to get answer and still got nothing.
The problem is I can build and run my application without any problems in IDE (IntelliJ 2021.3.2) but when I try to launch it from console like this:
PS D:\Programiki\Java\polish-draughts\target\classes> java com.polishdraughts.PolishDraughtsWindowed
I get this -> Error: Could not find or load main class com.polishdraughts.PolishDraughtsWindowed
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Does that mean it doesn't see required dependencies in classes folder? Are they required to be there? If so, how do I force IntelliJ to include them in target/classes? I don't understand why it runs in IDE using play button but doesn't want to start in powershell. Console version of this app, before it included any external dependencies was running from console just fine but now its the same problem. Is my module-info.java file wrong, dependencies don't get exported where they should or what is going on here? In pom.xml i have not specified any scope so it should be "compile" by default according to documentation. I'm totally clueless at this point. I tried to change external libraries to be dunked into lib folder but that didn't change anything. Tried invalidating cache, removing .idea folder, creating new project from scratch and copying just src content over, reloading libraries using "reload project"/"generate sources and update project". Removing iml file (now its completely gone and it doesn't seem to change anything - still app runs perfectly fine in IDE). Maybe I'm launching it wrong?? Please help...
You need to have all the required libraries on the classpath of the JVM.
When you run your JavaFX application in Intellij, and click on the 'Console' you'll see a line that starts /usr/lib/jvm/java-17-openjdk-amd64/bin/java (or wherever your Java installation is). You may need to click on an ellipsis (...) to see the full line.
The line will look something like:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java -Dfile.encoding=UTF-8 -classpath /home/tgdavies/.m2/repository/org/openjfx/javafx-controls/17.0.1/javafx-controls-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-graphics/17.0.1/javafx-graphics-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-base/17.0.1/javafx-base-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-fxml/17.0.1/javafx-fxml-17.0.1.jar -p /home/tgdavies/dev/scratch/javafxdemo/target/classes:/home/tgdavies/.m2/repository/org/openjfx/javafx-controls/17.0.1/javafx-controls-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-base/17.0.1/javafx-base-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-fxml/17.0.1/javafx-fxml-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-graphics/17.0.1/javafx-graphics-17.0.1-linux.jar -m com.example.javafxdemo/com.example.javafxdemo.HelloApplication
Of course the details will be different for your project. This command line will allow you to run your application.
In PowerShell, this works for me (surrounding parameters containing \ with " characters:
C:\Users\tgdavies\.jdks\openjdk-17.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar=55078:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\bin" "-Dfile.encoding=UTF-8" "-classpath" "C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-controls\17.0.1\javafx-controls-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-graphics\17.0.1\javafx-graphics-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-base\17.0.1\javafx-base-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-fxml\17.0.1\javafx-fxml-17.0.1.jar" -p "C:\Users\tgdavies\IdeaProjects\winjavafx\target\classes;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-controls\17.0.1\javafx-controls-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-fxml\17.0.1\javafx-fxml-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-base\17.0.1\javafx-base-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-graphics\17.0.1\javafx-graphics-17.0.1-win.jar" -m com.example.winjavafx/com.example.winjavafx.HelloApplication
This question already has answers here:
Why does Eclipse generate .class file if there is a syntax error in my Java source file?
(3 answers)
Closed 2 years ago.
When I try to run program.java in Eclipse IDE in spite of having an error, it shows the output in the console. But when I try to compile the same program using CMD, it throws a compilation error. I am learning and trying Abstract class. (I know we have to add abstract before class B) .
program.java in Eclipse IDE
compiling program.java in CMD
Eclipse IDE is caching the build of your program, so each time he does not compile fully again but only the changed source. Eclipse has this option that if you have an error while building, it will use an older build which compile so that you can execute it.
So i think that's the main issue.
Please, try to Clean all and then rebuild in Eclipse and see if it keeps running on it
For Eclipse you can find the rebuild option under Project > Clean and then select the project
Eclipse uses its own compiler that allows incremental compilation, building the project even if you have errors, and a few other things. It automatically builds every time you save, so it may have a previous version of your file (that works) already compiled.
The javac you are using from the command line is probably the Oracle or OpenJDK version, which do not let you build projects when you have any errors in them.
As for your actual code, you should either mark B as abstract, or actually implement the abstract display method declared in A, since only abstract classes are allowed to have abstract methods.
Every time you attempt to run your project and it still has build errors, Eclipse should show you a message asking if you want to continue despite those errors (unless you tick the box saying you don't want to see the message again). I don't recommend running projects in Eclipse even when you have errors - the compiler complains for a reason.
I have currently finished my java project in eclipse and now I would like to export it as an executable .jar. The strange thing is, that my program is working fine, when I run it in eclipse, but, unfortnuately, it is not working when I export it.
What should happen:
Well, I have set up a GlobalMouseListener with system-hook-2.5.jar. When I run it as an executable .jar I don't get any input from the listener.
Library is included (I'm working with system-hook-2.5.jar). Is this a problem? It is not the first time exporting a Java Project for me.
Any suggestions, what I could try?
It is always a good idea to start the program via console
Type in
java -jar <Filename>
I got an exception, that causes some problems.
I am a beginning programmer that has been using an IDE to learn to code. However I want to try using a text editor to see which one I would like more. My question is, once you download a text editor, what tools do you need to compile and run a program and how do you set them up?
You just need the JDK. Installing it (it has an installer) should set it up sufficiently.
When you want to compile something, use the javac tool:
> javac MyClass.java
When you want to run something, most likely you'll want the java tool (although of course if you're doing servlets or such, you'd use something else):
> java MyClass
Or of course, go ahead and use a separate text editor if you like but continue to use the IDE to compile, not least because presumably any IDE you were using had an integrated debugger, and a debugger is an essential tool.
If you want to use a text editor for java programming don't forget to add java to your Environment Variables Path. You can check if java is already added to your path by running java -version in your command line. If you get nothing you have to add C:\Program Files\Java\jdk1.8.0; to your path (your version of the jdk may be different, change it to the name you have). Don’t forget that semi-colon ; at the end.
Now you are set and ready to compile and run java programs just as explained by #T.J. Crowder in the answer above.
How can I compile and run a Java program?
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#win32-2b
Is English difficult for you? There are tutorials for your language too.
to compile:
javac path/to/file/with/main.java
then to run:
java path/to/file/with/main
I would suggest that if you are starting with the java language, first use an IDE (Eclipse, Netbeans, Intellij IDEA or another. For a first step let the IDE do his job. Then once you evolve quickly use a build manager (Ant, Maven, Gradle, ...). It is a good practice to always have a build manager in place to keep the build in control.
To compile (assuming you are in the same folder as the file is)
javac "filename.java"
To run
java "filename"
make sure you have set the environment variables correctly, so that the command line can detect where javac is.