run java program from cmd with class imports from jar files - java

Hey community I have this rookie problem so if anyone could help that would be great :)
So here it goes...
I'm trying to test the stanford NLP parser by trying to run the java demo file which is included in the file you can download from here
My problem is probably on the imports of the classes included in the .jar files..I tried the solutions on some other stackoverflow threads such as doing
javac -cp "jar/path/number/1";"jar/path/number/2" ParserDemo.java
but the
java -cp "jar/path/number/1";"jar/path/number/2" ParserDemo
command didn't work.
I also tried to extract every jar file in a common folder but then I got the bad class file error..
So what's the move here? How can I solve this ?
P.S. please don't propose as a solution to use the online version of the parser.
EDIT: I'm using Windows 8.1 just in case somebody needs to know

Since it was posted as a comment and I don't know if it would be visible for future reference I'm gonna write the solution down here as proposed by the user mlk
I fixed my java command to java -cp ".;jar/path/number/1";"jar/path/number/2" ParserDemo and it worked perfectly!
This is because the current folder (.) was not included in the classpath so Java could not see the ParserDemo.class file.

Related

Could not find or load main class hello

please, I really need some help with this! I'm working on my very first Java program - the classic "hello world" program. I saved the correct script on Notepad as Hello.java in C:\myProjects and used the command line to compile a Hello.class in the same folder. Even though the class is in the current directory, java still could not find it!
Here is a screenshot of the error message
Then I tried to manually set a classpath, even though I shouldn't need to do that, as seen here, but still the command line is not able to run the Hello.java program.
How do I fix this?? I even tried uninstalling and reinstalling my Java JDK altogether.
You need to capitalize Hello
Your command needs to be java Hello
Java is case sensitive.
or you might just install IntelliJ IDEA / Eclipse / NetBeans and enjoy life a little bit more :D
example step-by-step video (and there are tons more of those):
https://www.youtube.com/watch?v=ypLISBPwIWc

Java main class autocomplete

If I'm trying to run a java program and I don't know the exact name of the Main class, is there any way to use tab completion to figure it out?
java -cp stackoverflow.jar org.<tab>
stackoverflow serverfault stackexchange
java -cp stackoverflow.jjar org.stackoverflow.<tab>
Main IntegrationTest QuestionAnswerConsole
Something along those lines.
Basically you are asking how to configure shell autocompetion to support java. It is possible. Take a look on this discussion: How does bash tab completion work?
I have to say that this is a good idea not only for discovering the main class but also to complete other command line options and a class path. I'd be glad to use such script if you develop it. Good luck.
EDIT
At least on my Ubuntu file less /usr/share/bash-completion/completions/java exists and therefore some completion should work. You are always welcome to improve the script.
The only way to get the Tab Completion that you are talking about is to use a shell that is "Java aware" or a shell script that provides this feature for the java command. git has a similar feature, so I don't think it's completely impossible.
Edit:
According to this question on SU, it is possible to create an autocompletion script for the bash shell. Since the question on SU is slightly different than what you are asking, I don't see a lot of specific details that relate to this question. However, it looks like a good place to start.
I don't know any easy way to get the autocompletion of classes names if they are hidden in a jar file.
On the other hand, you can add a Manifest to your jar to make it auto-executable (ie., you just have to run java -cp ... -jar stackoverflow.jar) !
In the jar archive, add a META-INF folder, and inside that folder, create a MANIFEST.MF file that reads :
Main-Class: org.stackoverflow.Main
(or whatever your main class really is).
Here is some documentation : Setting an application's Entrypoint
Other answers suggest you solutions but as a sidenote, you don't need it if you provide maintainer of the jar file provides a manifest file with path META-INF/MANIFEST.MF. Then java automatically extracts main class from the manifest and you can run it this way:
java -jar stackoverflow.jar

Not sure where to put my Java library needed for my class assignments

I'm starting a Java programming class at UCSD next week and I'm trying to prepare over the weekend. There is a library called objectdraw.jar which comes with the book we will be using for our class projects. I am currently using Vim for my Java projects because most assignments will be simple and I am still trying to learn and master Vim.
Now to the question:
In C++ I am able to use #include with angled brackets <> to include headers in a default folder and #include with quotes "" to include local header files. It seems from what I've found with some Internet searches that you can't do that with Java.
I tried putting the objectdraw.jar file in the same folder as my .java file for the first assignment, but it doesn't find the library using the following code:
import java.awt.*;
import objectdraw.*;
When I searched online, the Gentoo HOWTO mentioned setting a CLASSPATH but then mentions that those instructions should be considered deprecated, but doesn't provide further instructions.
The website where I downloaded the library http://eventfuljava.cs.williams.edu/library.html does not provide instructions for Vim, but does for other IDEs like Eclipse and Netbeans.
Can anyone tell me where I would place the .jar file to ALL my projects can find it, or can anyone point me to a resource where I might find an answer. I seem to be asking the wrong questions when searching online.
Thanks!
Look here for how to set classpath using command line options to any of the java sdk tools - java, javac. The page shows how to set the environment variable CLASSPATH as well as how to use the switch -classpath.
In your case if you have the objectdraw.jar in the same directory as your source files, then you will need a command like this to compile -
javac -classpath . MyClass.java
and this to run
java -classpath . MyClass
You can also provide the full path to a jar file in the classpath and add multiple directories and jars. Such as -classpath .:/path/to/my.jar:/some/other/directory note paths and separators for *nix environment. on Windows it would be ; and \
Edit: Note comment below for quoting paths with spaces in them.

java -cp on Windows problem

Can someone please tell me why this java command in a .bat file won't run my java program on WinXP? I have all my jar files in a folder called lib and my class files in a package folder mypackage.
java -cp ".;.\lib\poi-3.7-20101029.jar;.\lib\jsr173_1.0_api.jar;..." mypackage.MyClassWithMain
I have tried all sorts of things to no avail. I get a ClassNotFound error as soon as the program attempts to use some of the classes in the jar files. So, I think there's something wrong with my -cp option. It does find my main().
I want to give this program to someone who doesn't know a thing about computers, so I want them to be able to double-click the .bat file and go.
I wrote everything on a mac without much problem.
I bet the moderator is going to slap me upside the head for this question, but I did search extensively for an answer to this.
Thanks you!
John
Sorry, I should have put in the entire command line:
java -cp ".;.\libs\jsr173_1.0_api.jar;.\libs\poi-3.7-20101029.jar;.\libs\poi-ooxml-3.7-20101029.jar;.\libs\poi-ooxml-schemas-3.7-20101029.jar;.\libs\resolver.jar;.\libs\xbean.jar;.\libs\xbean_xpath.jar;.\libs\xmlbeans-qname.jar;.\libs\xmlpublic.jar" excelsifter.ExcelSifterController
This is all on one line. I tried / instead of \, but that didn't seem to work. Everything I could find on this indicates that for windows you have to use the backslash.
All the dependencies are here, as far as I know. At least my mac doesn't complain when I use essentially the same command.
My directory containing my .bat file contains the excelsifter package (a folder called excelsifter) and the folder libs with all the jar files in it.
Thanks, John
Your starting string looks ok to me, try to check if there are other dependencies in the libraries you use as #Said mentioned. The best way is to search for class your java cannot find, probably you'll find it declaration in some other library you didn't included in your classpath.

Where do you add 'commons-codec-1.4.jar' to if you want access to encodeHexString etc?

Hey, this question links into another post I made today: Reading the first 10 bytes of a file in Java
Having been told that my solution could involve the apache commons codec library, I've tried to figure out where the file needs to be placed and am seriously not getting it. Can't find anything online that simply says 'It goes here...' so maybe someone on here can help?
I've got Windows 7, I'm using JCreator and have the 'jre6' and 'jdk1.6.0_23' folders in my Program Files/Java folder
Any help at all would be appreciated so please let me know :)
Thanks :D
You need to reference the jar file (not the containing directory) via your class path (a list of directories and .jar files)
e.g. from a command line:
$ java -cp {complete path to jar file} YourClass
Alternatively you can set the CLASSPATH environment variable.
See here for more details.
You may also specified globally CLASSPATH environment variable i.e:
unix:
export CLASSPATH=/path/to/jar1.jar:/path/to/jar2.jar: ...
windows:
set CLASSPATH=c:/path/to/jar1.jar;c:/path/to/jar2.jar
Remember on windows classpath separator is ";" and on unix/linux it is ":".
It is also correct for -cp option mentioned in previous answer by Brian

Categories