Using ClearNLP Semantic Role Labeler - java

I want to use clearNLP (http://clearnlp.wikispaces.com/) for extracting semantic role labels of an input sentence. I followed the instructions here: http://clearnlp.wikispaces.com/installation (I downloaded the jar files, put them in a directory called ClearNLP and set the classpath) but when I run the command java com.clearnlp.run.Version, I face the error: Could Not find or Load Main.
I tried it twice: Once I set the classpath as an environment variable of windows and ran the command in CMD. But, when it didn't work, I tried to create a java project, set the libraries using NetBeans and run the program. But, it didn't work, too.
BTW, when I run echo %classpath% command, I see that the classpath is set correctly.
Can anybody help me?

Try Eclipse. I included the jars in a new project I created. I then created a simple class like so
package test;
import com.clearnlp.run.Version;
public class TestClearNLP {
public static void main String(args[]) {
Version.main(args);
}
}
When run, this creates output in the console of:
ClearNLP version 2.0.2
Webpage: clearnlp.com
Owner : Jinho D. Choi
Contact: support#clearnlp.com
The only weird situation I ran into was that Eclipse did not like the jar files beginning with a period. I removed those from my project and ran with the remaining libraries.

Related

Java runs in eclipse and will compile, but wont execute on cmd, but still runs in eclipse. How can I get it to execute in cmd?

So I have a basic hello world set up in eclipse and I can compile it using cmd easily (I have set all the necessary paths), however when I then try to use the java command to execute the hello world, it always returns the same error:
Error: Could not find or load main class helloWorld
Caused by: java.lang.NoClassDefFoundError: net/codejava/helloWorld (wrong name: helloWorld)
This is the code used:
package net.codejava;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I am cd in the right directory (I think, I cd into the src directory and then into the package file stored in src) and am using Windows 10 with java 18.0.1 and JRE build 18.0.1+10-24
Any help would be greatly appreciated, as this is highly frustrating, when the code runs effortlessly on the eclipse console. Thanks.
Your file has a 'package' of net.codejava and a name of helloWorld, meaning, the full name of this class is net.codejava.helloWorld.
The java command, at least in the way you're using it, requires that you pass the full name, thus, you must run java net.codejava.helloWorld. Just java helloWorld simply isn't going to work.
But that's not all.
Java needs to then find the class file that contains the code for class net.codejava.helloWorld. It does this by first turning that full name into a path of sorts: net/codejava/helloWorld.class, and it will then scan each entry in the classpath for that. You can put directories and jar files on the classpath.
Thus, you have a directory on your system; let's call this directory X. X contains a directory named net, which contains a directory named codejava, which contains a file named helloWorld.class. If there is no such X (i.e. your class file is not in a dir named codejava for example), you're going to have to fix that by making these directories.
Then, X (and not the codejava dir!) needs to be on the classpath. Usually (it depends on how you configured things), 'the current dir' is by default on the classpath.
Given that your code is in, say, /home/PythonSux/workspace/learningjava/net/codejava/helloWorld.class, that means the dir that needs to be on the classpath is /home/PythonSux/workspace/learningjava. After all, if you, from there, look for net/codejava/helloWorld.class, you find the right file.
Therefore, either cd to that directory, or run java -cp /home/PythonSux/workspace/learningjava net.codejava.helloWorld
Note that this isn't usually how you actually run java apps. You either run them from your IDE, or you ask your build tool to run it, or you package your java app into a jar file and run that, etcetera.

Problems running Java from Atom

I currently don't have a working way to edit and run Java on my computer, so I'm trying to get Atom working with Java (I realize it's not a Java IDE and I'm not trying to make it one, I just want to be able to do some light Java work on my laptop). I've installed the script and instant-build packages for Atom and wrote the following test code in a file called "main.java' in my project folder:
class Main{
public static void main(String[] args) {
System.out.println("please");
}
}
When I try to run the code with cmd+i (I'm on a 2012 MacBook Pro) and get the following error message:Error: Could not find or load main class main.
I'd be happy to provide any further information; thanks for helping!
The huge problem of learning Java is that you need to launch the projects in a very strict way, and setting the classpath is always problematic. The solution lays in the following (pretty enigmatic) line of the "Script" documentation:
Project directory should be the source directory; subfolders imply packaging.
So, instead of opening a plain file, open the project (folder) with .java classes or define inside the file, the package to which your .java belong.
It's due to the fact that JDE needs to create a virtual target in form of .classess and single .java file definitely can't be launched as standalone file. I suppose that "Script" is not able to locate the source folder when you try to execute seperate .java file.
Before launching your .java files always "Add Project Folder..."
Please remember that it's not possible to have several folders opened if they don't belong to the same project. Such situation cause problems of locating the right classpath and in the end javac prompts the error.
You have to name your file with the first letter in upper case Main.java, since it must match the name of your class
I replicated the issue quite easily. If I created a new directory in Atom itself and then tried to run the code it didn't work as your error message came up for me as well. Error: Could not find or load main class main.
I used an existing directory and then created a file inside that folder in Atom and ran the same code it worked. I then copied and pasted that same file into the directory of my choice and it worked.

Maven project execution command

Below is my sample class file:
package org.foo.tutorial;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
In order to execute the project (maven framework) we run:
>java -cp Something-1.0.SNAPSHOT.jar org.foo.tutorial.APP
The above command works fine and gives me the output 'HELLO WORLD'.
However, if I leave out the third argument in the above command (org.foo.tutorial.APP) I get the following error:
Error: Could not find or load main class target.MavenTutorialApp-1.0-SNAPSHOT.jar
My question is:
Why should the groupId and app name matter when I am supplying the entire 'jar' file ?
The error is a bit misleading. Your java command is incorrect since you don't specify a class. The Something-1.0.SNAPSHOT.jar is meant to be part of the -cp option but java is interpreting it as the class.
That's how java behaves
The java command starts a Java application. It does this by starting a
Java runtime environment, loading a specified class, and calling that
class's main method.
If your .jar file contains an entry point specified by a Main-Class header in the manifest, then you can simply run
java -jar Something-1.0.SNAPSHOT.jar
Let me try to answer your question.
Why should the groupId and app name matter when I am supplying the
entire 'jar' file ?
Lets divide the question into smaller part -
What is group id? - The id of the project's group.
What is artifactId? - The id of the artifact (project) and off course version is part of default artifact name.
While running a java program under jvm there are no such effect that how you built jar, for example it is produced by maven or gradle build process or even command line.
Things that matter is jar file and entry point or the class where main reside.
This may be out of scope of this question, but i felt relevant.
To run java program from jar file, you can explicitly mention like above. Also you can create executable jar file by adding manifest file, where it define the entry point of the executable -
Main-Class: org.foo.tutorial.APP
How to create executable jar?
Also maven maven-assembly-plugin helps to package executable jar.
help

Why do I need to delete the package line to run Java outside of NetBeans?

So I'm completely new to programming, and I've been writing some Java with the NetBeans IDE. My code runs fine within NetBeans, but I've tried to run it using the command line as well. However, if I run it from the command line, I have to delete the line:
package firstprogram;
which NetBeans automatically places at the top of each new file, or I get the error:
Error: Could not find or load main class FirstProgram
However, if I do delete the line, then the program no longer runs within NetBeans! It doesn't seem right that I have to choose whether to run a .java from within NetBeans or without.
The research I've done makes me think that this is something to do with directory structure? But everything I read on that goes straight over my head. NetBeans has a structure with "build", "dist", "nbproject", and "src", but when I use the command line I just place the .java file in an empty directory and javac from there.
Any explanation is appreciated! The books and tutorials I'm learning from either assume you're just using NetBeans or don't have the package line at all.
You can compile your class using javac command from anywhere, as long as you provide correct relative or absolute path. The problems come when you want to run your program using the java program.
You have to provide the correct path corresponding to your package declaration. For example, if I had 'MyClass' in package mypackage, first line would look like this:
package mypackage;
class source stored on disk:
c:/MyNetbeansProject/src/mypackage/MyClass.java
Compiled bytecode:
c:/MyNetbeansProject/build/classes/mypackage/MyClass.class
Now, if I would have opened a command prompt/terminal in folder c:/MyNetbeansProject/build/classes/, I could run the program using java mypackage/MyClass or java mypackage.MyClass.
However, if i would be somewhere else, I would have to say where the class files are located using the cp option: java -cp c:/MyNetbeansProject/build/classes mypackage/MyClass. The path in cp option can be relative or absolute, use "" when it contains spaces.
Package are directory architecture.
If your class is in the package com.acme.test, the class should be in the com/acme/test directory.
Instead of placing your class in an empty folder, place it in a folder named firstprogram and do javac firstprogram/youclass.java
The package (and folder) permit you to arrange your architecture with logical pattern.
More info here : http://www.tutorialspoint.com/java/java_packages.htm
So like OcterA said, you should keep organized, but with only one class this is not the issue. I believe that your problem is that you are not entering the correct command into the command line.
First cd to the correct directory and when you want to execute a file within a package in that directory you need to enter
java packageName.className
In this case
java firstprogram.FirstProgram

Could not find or load main class

I have Windows 7, installed jdk1.7.0 and its supporting jre7.
My problem is compilation part works perfectly, but while running the Java program I get this error saying:
"Could not find or load main class"
I am storing all my programs in javalab folder. I have set the path to it. Procedure looks like this:
C:\Users\user>cd\
C:\>cd javalab
C:\javalab>autoexec.bat
C:\javalab>set path=C:\Program Files\Java\jdk1.7.0\bin
C:\javalab>javac p1.java
C:\javalab>java p1
Error: Could not find or load main class p1
C:\javalab>
I was having a similar issue with my very first java program.
I was issuing this command
java HelloWorld.class
Which resulted in the same error.
Turns out you need to exclude the .class
java HelloWorld
Try:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)
set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause
javac should know where to search for classes. Try this:
javac -cp . p1.java
You shouldn't need to specify classpath. Are you sure the file p1.java exists?
I had almost the same problem, but with the following variation:
I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
when was trying to run the project on PC 2 got the same error "Could not find or load main class"
I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03)
After restarting Ecplise it still didn't work
I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.
That's quite odd behavour, I cannot completely understand it.
Hope It'll help somebody. too.
i guess that you have a different class name in p1.java
Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.
If package is x.y
java x.y.p1
Here is my working env path variables after much troubleshooting
CLASSPATH
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)
bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)
Just a little checklist for people that have this problem still.
I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use
javac -cp . packageName/className
which means if you've had a package named def and main class with name TextFrame.java you'd write
javac -cp . def/TextFrame
omitting the trailing .java extension, and then you run it with the
java def/TextFrame
and if you have have arguments, then you need to supply it with arguments corresponding to your program.
I hope this helps a bit.
First, put your file *.class (e.g Hello.class) into 1 folder (e.g C:\java). Then you try command and type cd /d C:\java. Now you can type "java Hello" !
You might have the CLASSPATH environment variable already added!!
Use following to avoid further usage of -cp . in java -cp . CLASSFILE
Add . to CLASSPATH in system properties->environment variables or by cmd
set CLASSPATH=%CLASSPATH%;.;
I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.
I think this is because it tries by default to run it in some other configuration which causes problems.
Hope this answer helps some.
If you have a single .java file to compile using command-line , then remove
topmost package parts from the code, the compile again, it will work.
This worked for me.
Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:
Could not find or load main class xxx Linux
I just delete that reference and added again and it worked fine again.
i had
':'
in my project name e.g 'HKUSTx:part-2'
renaming it 'HKUSTx-part-2' worked for me
You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours

Categories

Resources