java -cp on Windows problem - java

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.

Related

Java on Win server, bat run packages

To begin with, I dont know much Java, but we have some old Java applications on my firm that collects information that runs every night, however recently it stopped working. There is a bat file that, from what I understand, runs all the required packages, however, when I run it, it returns "Could not find or load main class -insert whatever class I put second - "
Code from the .bat file:
java -cp com.spprod.mywysiwyg.NY.jar; mysql-connector-java-3.1.10-bin.jar; com.spprod.mywysiwyg.CalculateDailyStats;
The first one is the whole package, and the second one is the connector, they are both in the same folder, but in the cmd I get the error that it cant find mysql. If I switch order with 2nd and 3rd it says that I cant find "CalculateDailyStats" instead. Is there something wrong with this line of code?
Thanks in advance.
The -cp switch specifies the classpath in java (path where to look for the files). The jars after -cp are separated with semicolon.
Now, I suppose, that you are trying to run com.spprod.mywysiwyg.CalculateDailyStats class. So, maybe the last semicolons are confusing e.g. try the following:
java -cp com.spprod.mywysiwyg.NY.jar;mysql-connector-java-3.1.10-bin.jar com.spprod.mywysiwyg.CalculateDailyStats

How to run or call an exe file from a JAR file which is present in the same directory?

Hello all :) I love stackoverflow where I always find answers but this time I could not so personally asking... Its bit lengthy please go through it.
I am creating a java application where one of my resource is an exe file which I need to call in the java code. But later I would convert the whole java code to a JAR file... and I would add the JAR file and the exe file for the Setup file for installation process. So when I extract the files I want my JAR file to call the exe file while running... I am doing this all in Eclipse :)
So my doubt comes here which path I should put up in the java code... ? So that it will always call the file the exe file from the same directory where the JAR file is also present.. :)
Any help would be great :) Thank you in advance :)
Assuming that you control the whole installation process; and that you also control the script that later starts a JRE to run your JAR within; my suggestion would be: simply use a property here.
In other words; your installer knows that it copied JAR and EXE to D:\example\ for example. Then just make sure that your JAR is started like:
java -jar D:\example\your.jar -D your_path=D:\example
(this is just meant as example, you would have to work that out, probably the \ in there need some special treatment for example)
Then your application can simply query for that system property "your_path" and take the value from there.
Alternatively, you could try this solution that works "pure java".

run java program from cmd with class imports from jar files

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.

Running java from a bat file in such a way that path doesn't need to be specified

I think I have seen this done, but am not sure where. What I want to do is to create a bat file I can package with my class files when sending to a friend to show them progress/ask advice on non programming matters. My friend is not very handy when it comes to code and doesn't like changing computer settings. Just using java myClass as a command line won't work here because although my friend does have java installed, he has not set his windows environment variables so his command prompt knows where to find java.
What kind of line would I need to add to my batch file to make it so it can compensate for problems like this?
Create a manifest file (manifest.txt):
Main-Class: com.mycompany.myapp.MyMainClass
Package your app as a jar:
jar cfm myjarfile.jar manifest.txt *.class
Create a batch file:
start myjarfile.jar
If it is about sharing and running a single java file without jar dependencies. And you are only worried about the java runtime environment setup, then you can use online java code compilers and executors. Here is one:
http://javalaunch.com/JavaLaunch.jsp
You can google for more!
Use an IDE, NetBeans or eclipse and package your files as a Jar file.. that can be executed directly and you do not need to worry about dependencies, other classes or libraries.

where to place jar in order to run a program from command-line?

this will sound silly but i am executing my code from command prompt and have to use a jar in my class.
I have placed my jar in lib folder of JDK..
but i am still getting error of file not found
any explanation??
EDITED : guys tried all but still not working
EDIT 2 :i am trying to work as was told by this link i am using js-1.6R5.jar
Edit 3 : i undestand all the things you ppl have told but nothing working for me.. pls give me a link to upload my example that i can share with you all.
Edit 4 : i am fed up by setting classpaths but its not working... i have SDK installed in my system, do i need an extra JDK to run my programs from command prompt??
You need to add the jar to the class path by doing the following...
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Please see Wikipedia - Classpath_(Java)
You can place it anywhere, as long is you include it in your classpath. See Setting the Class Path for how to include jars in the classpath.
Have in mind that adding something in the JDK lib is almost never a good idea.
You can make a lib folder in your application's directory and put jar files there, then make your application find them by adding lib to your application's classpath.
And, don't put your jar files in JDK's lib folder. It's not good practise.
You need to let Java know that you want to include the jar in your classpath (the list of folders and jars it checks for classes). One way to do this is with the -cp command line argument, something like
java -cp ".;pathToMyJar\myJar.jar" MyClass
Another is to edit the CLASSPATH environment variable in your OS to include your jar.
A simple solution will be to place the jar fiel inside the windows folder if you are doing it in a Windows machine.
Unfortunately your question contains a lot of question signs and few information.
If you are using java.io.File to open jar as a regular file this jar should not be in lib directory. You just have to provide correct path in file system.
If however you are just trying to use jar as a part of your application it should be in classpath. Use either command line java -cp myjar.jar MyMainClassor put full path to this jar to global environment variableCLASSPATH`.

Categories