ClassNotFoundException on simple Hello World in IntelliJ - java

I am using IntelliJ, and get a ClassNotFoundException, even when doing a simple Hello World program. After googling and looking at how to add directory to classpath in an application run profile in intellij idea?, I tried changing my dependencies to the JDK folder (/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk), and to no avail.
I have also tried re-installing the JDK and re-installing IntelliJ, both to no avail, and I can run programs in NetBeans without issue. What's going on? Is this some strange bug with IntelliJ?
Code:
public class AppMain {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Error:
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/JObjC.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk:/Applications/IntelliJ IDEA 13 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain AppMain
Exception in thread "main" java.lang.ClassNotFoundException: AppMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1

I've found out what sorted it. It was a bug with my import settings, deleting my preferences (i.e. going to ~/Library/Preferences and doing rm -r IdeaIC13) sorted this issue out. Hopefully that's helpful to anyone else coming across this issue.

Related

Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/Predicate

I have created a jar file using
mvn assembly:assembly -DdescriptorId=jar-with-dependencies
and I run it on windows and it works fine and works as expected. Then I run it on Ubuntu and it gives the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/Predicate
at Maxima_ImageJ.run(Maxima_ImageJ.java:13)
at Maxima_ImageJ.main(Maxima_ImageJ.java:27)
Caused by: java.lang.ClassNotFoundException: java.util.function.Predicate
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
I have no idea why it works differently in Windows and Ubuntu. If someone do, please help. Is it related to the versions of java?
Set class path to Java 1.8 as java.util.function.Predicate is part of JavaSE8 and will not be available in 1.7 some of the set commands to use before executing maven command.
set path=C:\Program Files\Java\jdk1.8.0_05\bin//UPTO Bin
set JRE_HOME=C:\Program Files\Java\jre8//Upto Root folder of JRE
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_05//UPTO Root folder of JDK
Check your java version by using below command.
readlink -f $(which java)
If it is less than 1.8, then you have to update Java_Version.
One way is to edit in .bashrc file.

java.lang.ClassNotFoundException with IntelliJ

I've been searching for a solution that works but none have so far. When I try to run a program that is known to work for others, I get this error:
"C:\Program Files\Java\jdk1.7.0_65\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_65\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_65\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain gui.WFPaLM
Exception in thread "main" java.lang.ClassNotFoundException: gui.WFPaLM
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Process finished with exit code 1
The directory "src", which contains all of the packages, is set as the Source Folder.
Edit: http://imgur.com/O45zm4t,MPYuDCa that is what my project structure looks like.
Edit 2: I downloaded the community edition of intellij, and it worked. It's a mystery as to why.
Have you compiled the project before running? (Build > Make Project Ctrl+F9). Normally IntelliJ IDEA compiles a project automatically before running, but perhaps you have disabled this.

Running a java program via nailgun

Can someone please explain how do I start a program called main.java using Nailgun. It's been a complete nightmare for me and I cannot find any sort of tutorial on the web which explains it clearly.
Do I need to include anything extra in my program to run them?
I'm trying to follow the instructions given on the Nailgun website where it says:
Launch the Nailgun server with "java com.martiansoftware.nailgun.NGServer"
But the output I get is:
Error: Could not find or load main class com.martiansoftware.nailgun.NGServer
Also, when I try to run my compiled java program, the output is:
java.lang.ClassNotFoundException: main
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at com.martiansoftware.nailgun.NGSession.run(NGSession.java:261)
I think the idea with nailgun is that you have to include the classpath when you launch the server, like this:
$ java -cp /usr/share/java/nailgun-0.9.0.jar:path/to/myapp.jar -server com.martiansoftware.nailgun.NGServer 127.0.0.1 &
NGServer started on 127.0.0.1, port 2113.
[1] 22443
$ ng-nailgun com.user4215685.main

classpath error in java

I have written a java program where I am using a .jar library (Jinterface). I compile it using:
javac -classpath lib/OtpErlang.jar Game.java Actions.java
where the lib dir is inside my project folder.
There is no error when compiling but I get a runtime error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ericsson/otp/erlang/OtpErlangObject
at Game.main(Game.java:7)
Caused by: java.lang.ClassNotFoundException: com.ericsson.otp.erlang.OtpErlangObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
And I think that it has to do with the classpath, what is wrong?
Here is the Game class:
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
Actions server = new Actions();
server.moveRight();
}
}
Try running it like this, from the root of all your java packages:
java -classpath lib/OtpErlang.jar Game
This command did the trick:
java -classpath lib/OtpErlang.jar:. Game
I have missed the :. (which tells where my main class is, I suppose...)
Try like this.I hope this will help.....
javac -cp . lib/OtpErlang.jar Game.java Actions.java

Cant compile and run java code in IntelliJ IDEA

Using InteliJ IDEA version 12.1.6 I wanted to run the example from the Horstmann's 'Core Java' book:
public class Welcome
{
public static void main(String[] args)
{
String[] greeting = new String[3];
greeting[0] = "Welcome to Core Java";
greeting[1] = "by Cay Horstmann";
greeting[2] = "and Gary Cornell";
for (String g : greeting)
System.out.println(g);
}
}
But I get the following error:
"C:\Program Files\Java\jdk1.7.0_45\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\IntelliJ IDEA\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Welcome
Exception in thread "main" java.lang.ClassNotFoundException: Welcome
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
At the same time when compiling and executing a file in the command line everything is working correctly. Excuse me for such a question, I just started learning Java. :)
Sergey, it's look like you are working wrong with Idea. There is a step-by-step tutorial at wiki, that describes some basic concepts, about roots, run configurations, sdk-s and dependencies.
It will be good, if you look at it. (Exploring the project structure and Building(Running) the project especially)
IDE is a great helper to learn language, especially such intellegent one, as Idea, so, it will be a good start, to get familiar with it.
Basically, for a start, all your code(not compiled classes) must be under the source roots, and runConfiguration must specify application entry point (class with static void main() method).
You shouldn't think about class files, jar archives or command line tools, IDEA can do all this for you.
Better concentrate on learning, as I think!

Categories

Resources