Running java program using packages in multiple folders - java

I'm trying to run a program with the following structure:
+src
+gui
-XL.java
-moreFiles.java
+menu
-guiFiles.java
+util
-utilFiles.java
+extra
-extraFiles.java
I'm trying to compile the code by calling
javac gui/XL.java
Which succeeds.
When I try running the code with
java gui.XL
I get the following error message:
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
at gui.XL.<init>(XL.java:25)
at gui.XL.main(XL.java:58)
Caused by: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
... 2 more
Caused by: java.lang.ClassNotFoundException: java.lang.invoke.StringConcatFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I guess I have some problems with my classpath but I have no clue about how to fix it. Does anyone have any suggestions?

My problem had nothing to do with classpaths or that I wasn't using an IDE. My problem came from the fact that I was changing the 'java' command to run my java-openjdk-8 but my compiler still ran with java-openjdk-9. The solution was therefore
sudo update-alternatives --config javac #Change to java 8
sudo update-alternatives --config java #Change to java 8

Related

Mac JDK11: Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException

New to Java here. Using IntelliJ. I wrote a simple "helloworld" Spring application that compiles just fine, but when I run it receive the following error:
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51884:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -p /Users/winston.kotzan/Development/java-spring-helloworld/bin:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aop-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aspects-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-beans-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-indexer-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-support-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-core-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-expression-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-instrument-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jcl-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jdbc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jms-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-messaging-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-orm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-oxm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-test-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-tx-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-web-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webflux-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webmvc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-websocket-5.1.3.RELEASE.jar -m SpringHelloWorld/com.wakproductions.MainApp
Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException
at spring.core#5.1.3.RELEASE/org.springframework.core.Constants.<init>(Constants.java:67)
at spring.beans#5.1.3.RELEASE/org.springframework.beans.factory.xml.XmlBeanDefinitionReader.<clinit>(XmlBeanDefinitionReader.java:102)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:83)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:622)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:518)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at SpringHelloWorld/com.wakproductions.MainApp.main(MainApp.java:8)
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 9 more
I suspect that it may have to do with the CLASSPATH. I tried setting the CLASSPATH environment variable, as well as adding this to the java command
-cp /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
But that does not seem to work. Might I have the incorrect CLASSPATH?
The comments about the module-info file led me to some clues. I took notice that IntelliJ seems to have created a module-info.java file with the following contents:
module SpringHelloWorld {
requires spring.context;
}
Did not realizing I was compiling a module. I deleted that file along with everything in the bin directory. Then I was able to run it by the Run menu using this option:
I can run it on the command line via:
java -Dfile.encoding=UTF-8 -classpath /Users/.../java-spring-helloworld/bin:/Users/.../<spring-framework-libs>... com.wakproductions.MainApp

Code running in Netbeans but not in command line

I downloaded the time series classification toolbox available here that I succeed to run with Netbeans (not with Eclipse due to compatibility problems) using the command
netbeans --jdkhome /usr/lib/jvm/java-1.8.0-openjdk-amd64/
I would like to be able to run it on a server so I need to be able to run it using command lines.
However I didn't succeed. My guess is that there is a problem with jdk (I'm not used to Java so it might be something else), since when I run
sudo update-alternatives --config java
I get
There are 5 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 auto mode
1 /usr/bin/gij-4.8 1048 manual mode
2 /usr/bin/gij-4.9 1049 manual mode
3 /usr/bin/gij-5 1050 manual mode
* 4 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
5 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 manual mode
Which does not contains the path to the jdk I use with Netbeans.
I didn't find a way to tell Java that I wanted to use another jdk, so I tried without:
Example where I run a specific main file
From TimeSeriesClassification/src:
javac timeseriesweka/classifiers/LearnShapelets.java
java -cp ../bin:. timeseriesweka.classifiers.LearnShapelets
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: TechnicalInformation
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: TechnicalInformation
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Any help on how to set up correctly Java will be appreciated
With (following the answer of ACV)
java -cp ../bin/Test.jar:lib/*:. timeseriesweka.classifiers.LearnShapelets
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
at timeseriesweka.classifiers.LearnShapelets.main(LearnShapelets.java:746)
Caused by: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
... 1 more
Caused by: java.lang.ClassNotFoundException: java.lang.invoke.StringConcatFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Try this:
-cp "../bin/Test.jar:lib/*:." instead of your:
-cp ../bin:.

Signapk.jar giving error java.lang.ClassNotFoundException: sun.misc.BASE64Encoder

I have tried to make my app system app and i converted successfully on my windows machine but when I tried to run following same command on mac machine it kept on giving error. I have downloaded signapk.jar from different sources as well not able to run the command.
java -jar signapk.jar platform.x509.pem platform.pk8 device-owner.apk device-owner_sign.apk
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder
at com.android.signapk.SignApk.addDigestsToManifest(SignApk.java:169)
at com.android.signapk.SignApk.main(SignApk.java:325)
Caused by: java.lang.ClassNotFoundException: sun.misc.BASE64Encoder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
Looks like the app you're trying to run isn't JDK 9 compatible.
Try again using Java SE 8.

Encountered error while trying JCov java coverage utility

Almost everywhere in INTERNET there are these basic steps:
• Compile java files as usual
javac <source-files>
• “Instrument” the byte code
java -jar jcov.jar Instr <application classes>
• Run the code
java -classpath ...:jcov_file_saver.jar ...
• Create a report
java -jar jcov.jar RepGen <jcov xml file> demo
I was able to instrument class files and jar files both, but couldn't run jar one.
Faced this error:
$ java -cp .:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tdk/jcov/runtime/Collect
at BubbleSort.main(BubbleSort.java:49)
Caused by: java.lang.ClassNotFoundException: com.sun.tdk.jcov.runtime.Collect
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
)
Can anyone please help me out or direct me to some web-page where I can understand this?
You also need to provide the jcov.jar file as well because it contains the com/sun/tdk/jcov/runtime/Collect class.
To supply the jcov.jar at run time, run this
java -cp .:$JCOV/jcov_file_saver.jar -Xbootclasspath/a:$JCOV/jcov.jar -jar BubbleSort.jar
Using this resolved the issue faced thanks to sumedh's answer which pushed me to understand about various kind of classpaths.
Executing instrumented jar:
java -cp . -Xbootclasspath/a:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar

Error running and compiling ParserDemo2

I'm trying to programatically generate dependency trees using the Stanford Parser, following the guide to running ParserDemo found at http://nlp.stanford.edu/software/parser-faq.shtml
I run the line:
java -mx200m -cp "stanford-parser.jar:." ParserDemo2 englishPCFG.ser.gz testsent.txt
which results in
Could not find or load main class ParserDemo2
This makes sense, I need to compile, so I run
javac -cp "stanford-parser.jar:." ParserDemo2.java
which generates a .class file.
However, when I run it using the above line, I get:
java -mx200m -cp "stanford-parser.jar:." ParserDemo2 englishPCFG.ser.gz testsent.txt
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at edu.stanford.nlp.parser.common.ParserGrammar.<clinit>(ParserGrammar.java:46)
at ParserDemo2.main(ParserDemo2.java:25)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
What am I doing wrong? Something pretty basic?
stanford-parser.jar, has dependency on other jars. You need to add them to classpath.

Categories

Resources