Cant compile and run java code in IntelliJ IDEA - java

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!

Related

MPJ Express (Java MPI) running in Intellij IDEA

I downloaded mpj-v0_44 and extracted it to C:\mpj
Put Windows system env. variables MPJ_HOME to C:\mpj and in PATH added value C:\mpj\bin
I added mpi.jar, mpj.jar in Project Structure -> Libraries
and wrote simple helloworld mpi program:
import mpi.MPI;
public class Main {
public static void main(String[] args) {
MPI.Init(args);
int me = MPI.COMM_WORLD.Rank();
int size = MPI.COMM_WORLD.Size();
System.out.println("Hello world from <"+me+"> of <"+size+">");
MPI.Finalize();
}
}
I created run configuration described on picture:
But I get the following error:
MPJ Express (0.44) is started in the multicore configuration
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at runtime.starter.MulticoreStarter$1.run(MulticoreStarter.java:281)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: 0
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
... 6 more
I found similar problems in eclipse posted but none solution worked:
java.io.FileNotFoundException: null\conf\wrapper.conf (The system cannot find the path specified)
"Unresolved compilation problems" from running MPJ express in Eclipse Indigo
In Eclipse everything works fine but I need Intellij IDEA!
PS:
(if you need javadoc add whole C:\mpj\lib folder and C:\mpj\src folder for library sources)
You need to change the main class to
"runtime.starter.MPJRun"
and VM options to include your main class
"-jar $MPJ_HOME$\lib\starter.jar nz.geek.colin.mpj.Main -np 4"
This is because MPJExpress runs and then loads your program in order to create multiple processes.
If IntelliJ complains about runtime.starter.MPJRun not found you can ignore it but if the warning bothers you you can add the starter.jar to the project.

add library to java, CLASSPATH, jar, linux

I've been browsering the Internet for a couple of hours and I am unable to find an answer to my question. The library I'm trying to add is JGraphT
I'm new to Java and I wanted to add a free graph library. I downloaded all .jar files and then the issues startet. What step by step should I do?
I found information about compiling with -cp or -classpath or addng .jar to CLASSPATH (I'm using Linux and I write my programms in gedit (obligatory for my studies) and compile it with terminal). But I wonder what should I do step by step?
What do I have so far:
I have downloaded multiple .jar and they all sit in one folder with
the xxx.java file I would like to compile
do I need to change CLASSPATH? How to do it? How should I compile and
run my program after changing CLASSPATH? The ordinary way(javac
xxx.java; java xxx) or should I change sth?
or maybe I don't need to change CLASSPATH just add -classpath while
compiling? If so, what should the compile anr run commend look
like?
Also I have already tried using -cp... I'm enclosing my lines in terminal. It compiled correctly, but when I tried to run it, I received strange errors. I'm sure the code is correct since it was given in the library as a way to test wether or not is it installed correctly.
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ javac -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT.java
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ java -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT
Exception in thread "main" java.lang.NoClassDefFoundError: HelloJGraphT (wrong name: org/jgrapht/demo/HelloJGraphT)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$
Let's say your class belongs into the package net.example.graph. This means the real name of the class is net.example.graph.HelloJGraphT.
This also means you have a directory structure like this:
project_dir/net/example/graph
Go to the project_dir folder, then try this:
javac -cp <path to jgrapht JAR> net/example/graph/HelloJGraphT.java
java -cp <path to jgrapht JAR> net.example.graph.HelloJGraphT

Java SQLite org.sqlite.JDBC classpath broken?

I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC
my code compiles and runs fine on Windows.
But when I tried moving it to Ubuntu it started showing this:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
at mall.AllegroReader.<init>(AllegroReader.java:33)
at mall.Mall.main(Mall.java:31)
I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar
with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk.
I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.
I have no idea what is going on. Pls help.
Here is what happens inside my dist directory:
work1#workwork:/var/www/mall/dist$ ls
mall.db Mall.jar Mall.jar.old sqlite-jdbc-3.8.4.3-SNAPSHOT.jar
work1#workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall
The classpath is ignored when you use -jar.
You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.
Error: Could not find or load main class Mall.main. all files are there,
my main class comes from Mall.java and is in mall package which
compiles to Mall.jar
So the correct command line is:
java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
OP findings
to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall
it showed
mall/Mall.class
so I should have used mall.Mall as the class to run (instead of pulling my hair)
After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large!) I decided to write the offending piece of code in PHP.
It proved to be more portable than Java in my case.

ClassNotFoundException on simple Hello World in IntelliJ

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.

classifier4j class not found exception on addMatch() method

i have a strange behaviour when trying to use bayesian classifier class from classifier4j package
basically my code is:
String test_string = "some bla bla web designers in here whatever";
SimpleWordsDataSource wds = new SimpleWordsDataSource();
BayesianClassifier classifier = new BayesianClassifier(wds);
wds.addMatch("Web Design")
Double d = classifier.classify(test_string);
what i'm getting when trying to run this code is:
run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.classifier4J.bayesian.WordProbability.calculateProbability(WordProbability.java:167)
at net.sf.classifier4J.bayesian.WordProbability.setMatchingCount(WordProbability.java:138)
at net.sf.classifier4J.bayesian.WordProbability.<init>(WordProbability.java:115)
at net.sf.classifier4J.bayesian.SimpleWordsDataSource.addMatch(SimpleWordsDataSource.java:94)
at tothego_classifier_test.Main.main(Main.java:44)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 5 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
which is pretty annoying since i've been trying to run a simple hello-world test (as in the website's usage part). I'm on netbeans 6.9 and i've ofc included classifier4j 0.6 jar and optional jar, also have the correct import statements in my main class.
I know Nick Lothian contributes here, so i tought would be good asking!
thanks everybody for efforts
ok i solved the problem, and here we go with a quick howto for everyone:
http://commons.apache.org/logging/download_logging.cgi click on this link and download the binary file
unzip the file you just downloaded
add commons-logging-xxx.jar, commons-logging-adapters.jar, commons-logging-api.jar to your CLASSPATH / project libs (i tried an awful lot, and it only seems to work with the whole jars included)
rebuild and rerun
hope this is helpful :)

Categories

Resources