How to use Jlex and Cup tools - java

i'm trying to learn something about JFlex and Cup tools on Ubuntu, because i have to use them for a school project. So i downloaded and installed JFlex and Cup. There are some examples in "jflex-1.6.1/examples", so i tried to run one of them. In particularly i tried to run the one in "jflex-1.6.1/examples/cup", following the instructions in the README file:
To compile:
jflex lcalc.flex
java java_cup.Main < ycalc.cup
javac Main.java
To run:
java Main test.txt
Files:
"Main.java" demo of a main program
"Makefile " makefile to compile and test the example
"README"
"lcalc.flex" the lexer spec
"output.good " how the output should look like for the test
"ycalc.cup" the parser spec
"test.txt " sample input for testing
With many difficulties, i have been finally able to compile, then to complete successfully the first three instructions. Now i have errors when i try to run: java Main test.txt. In particular i get this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: java_cup/runtime/Scanner
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: java_cup.runtime.Scanner
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
I don't have any idea on how to solve this. I tried also with other examples but the results were the same.
I hope that you can help me. Thank you!

I also got this error, and it's because I forgot to include java-cup.jar and java-cup-runtime.jar in my class path.
Try compiling like this:
javac -cp path/to/java-cup.jar Main.java
And then run your program like this:
java -cp path/to/java-cup-runtime.jar Main

Related

Compiled JAVA project, can't make it work

got a problem, can't manage it to work for me right.
Completed my java project and i want it to compile and run for a test. I do compile it as "Jar file" (not runnable jar file). I compile it through Eclipse IDE.
I do not setup any more references in compiling settings, making everything stock. Once i compile it as "Server.jar" in my desktop i'm trying to run through console, because it's more like console program. I'm using macOS.
Using command : java -jar Server.jar
Error:
Ervinass-MacBook-Air:Desktop Ervinas$ java -jar Server.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
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: org.json.simple.parser.ParseException
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
What should i do?
It shouws class is not found if i'm right?
This : org/json/simple/parser/ParseException is not recognised.
What i have tried : java -classpath ~/Documents/Java/Server/src/server/json-simple.jar -jar Server.jar
As all of you see, i'm using json-simple.jar library, moreover i do use mysql library, so i guess later it will be the same problem with mysql library.
Any solution? Tired of getting it working now..

JNI error has occurred when running java with OpenCV from terminal

I'm trying to compile a java-program written with OpenCV 3.2.0 from the terminal. It was originally written in Eclipse with OpenCV as a user library.
I compile it with this command:
javac -cp /opencv-3.2.0-java/build/bin/ *.java
It works with no error, the -cp command shows the path to the OpenCV jar. I then try to run everything with this command:
java -Djava.library.path="/opencv-3.2.0-java/build/lib" -cp jar/opencv-320.jar -classpath . Main
It gives me this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Mat
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: org.opencv.core.Mat
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)
I searched for solutions to this problem and came to the conclusion that it had something to do with native libraries. I use the -Djava parameter to link to the path of the library files from OpenCV, but it still doesn't work somehow. How do I fix this?
EDIT:
All the .java files are on a folder on the desktop. This is were the Main file is included. For example:
home/pi/Desktop/MultiSensor_v2.0_Java/Main.java
The path to the OpenCV jar:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar
The path to OpenCV native libraries:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib
-cp is short for -classpath. So I suspect that when you use both, the second one overwrites the first.
You can have multiple entries on the class path separated by : as stated in the documentation: Setting the class path. (actually, it looks like that is a typo in the doc, and it should be ; as a separator. You could try both)
If you execute your command from the directory home/pi/Desktop/MultiSensor_v2.0_Java/ that would make:
java -Djava.library.path="opencv-3.2.0-java/build/lib" -cp .:opencv-3.2.0-java/build/bin/opencv-320.jarr Main

JUnit exception in main?

I am trying to run a simple junit test using OS X and terminal. I have placed the Junit jar file inside my java folder. I was able to compile all of the files using:
javac -cp .:"/Library/java/junit.jar" *.java
It compiles just fine, with no errors. However when I try to run the command:
java TestRunner
It gives the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
at TestRunner.main(TestRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
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
I cannot seem to find what I am doing incorrectly. Any help solving this would be greatly appreciated.
Like #ToddSewell said, the external libraries should be in your classpath for the execution too. Try this:
java -cp .:"/Library/java/junit.jar" TestRunner

Import package from subfolder: Exception in thread "main" java.lang.NoClassDefFoundError

I have this directory structure:
src
MyPackageFile1.java
MyPackageFile2.java
vendor
something.jar
Example.java
Note that all files under src have a package com.example; declaration, while Example.java doesn't belong to that package: it is an example of usage of that package (so it has import xyz.pushpad.*;).
I successfully compile with:
javac -classpath vendor/something.jar src/*.java Example.java
However when I run:
java Example
I get:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/DeliveryException
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: com.example.MyPackageFile1
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)
... 7 more
If MyPackageFile1.java and MyPackageFile1.java are declared in package com.example, then there are three problems:
Your source directory structure is wrong. It should be this:
src
com
example
MyPackageFile1.java
MyPackageFile2.java
You are using the wrong classpath separator. Since you are using Linux (based on the pathname syntax), the classpath separator is : not ;.
Classpath you are using to run the code is wrong. The java command line should be something like this:
java -cp vendor/something.jar:src:. Example
However, this is all very hacky.
Unless your codebase needs to be modularized as multiple code trees, then you should put your Example source code into the src directory; e.g.
src
Example.java
com
example
MyPackageFile1.java
MyPackageFile2.java
It is a bad idea to put your source code and compiled code into the same tree. A better idea is to use the -d option; e.g.
mkdir bin
javac -cp bin:vendor/something.jar -d bin
src/Example.java
src/com/example/MyPackageFile1.java
src/com/example/MyPackageFile12java
java -cp bin:vendor/something.jar Example
You should be using a build tool of some kind ... if you want your builds to be consistent and reproducible.
Similar to the compile command the run command also needs the jar file on the classpath
java -cp vendor/something.jar;src com.example.Example
where Example.java is moved to new directory src/com/something

openmpi + java, could not find or load main class

I am recently trying out the java binding of the openmpi library :
I have succesfully compiled the lib with the following configurations:
$/configure --prefix "/home/yuechuan/Application/.openmpi" --enable-mpi-java --with-jdk-dir="/usr/lib/jvm/java-8-oracle/" --with-jdk-header="/usr/lib/jvm/java-8-oracle/include/"
$sudo make install
I am able to compile a simple java program with : $mpijavac src/com/cyc115/pa2/Main.java
currently $echo $CLASSPATH gives /home/yuechuan/Github/parallel_pa2/src/com/cyc115/pa2/
However I am having trouble to get the class file to run. Here are some alternatives I have tried so far:
$java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/mpi.jar -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar Main
which returns Error: Could not find or load main class Main error.
the pure and simple $java Main command returns NoClassDefFoundError
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: mpi/MPIException
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: mpi.MPIException
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)
... 7 more
Here's the java file if anyone needs :
package com.cyc115.pa2;
import mpi.MPI;
import mpi.MPIException;
public class Main {
public static void main(String args[]) throws MPIException
{
MPI.Init(args);
int rank = MPI.COMM_WORLD.getRank();
int size = MPI.COMM_WORLD.getSize();
System.out.print("hello world");
MPI.Finalize();
}
}
Any ideas on how I can get the .class to run?
Since the package is :
package com.cyc115.pa2;
You need to put the file Main.java in a sub-directory named com/cyc115/pa2
The java library path should be a path to the directory containing libmpi_java.so rather than the path to mpi.jar.
To compile:
javac -cp /home/yuechuan/Application/.openmpi/lib/mpi.jar com/cyc115/pa2/Main.java
To run:
java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/ -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar com.cyc115.pa2.Main
The following is not the problem, just an FYI:
It should be --with-jdk-headers with an s rather than --with-jdk-header however you do not have to specify it at all if you are already specifying --with-jdk-dir.

Categories