Java on Win server, bat run packages - java

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

Related

NoClassDefFoundError when running a java class through the command prompt

I've just completed quite a large project using IntelliJ and said I'd give the command line statement for people without an IDE to run it (the project will be run on different machines by different people etc). I haven't used the command line in a while and so I'm a bit rusty. I've gotten a NoClassDefFoundError:wrong name and I've been looking at questions on S/O such as Why am I getting a NoClassDefFoundError in Java? but these don't seem to fix my problem. I'm using quite a few external libraries but I have them correctly imported when trying to run. There are many classes compiled but only one will be run so I presume I only run the "java" command on the Main class
java -cp
.;poi-3.17/lib/comms-codec-1.10.jar;poi-3.17/lib/commons-collections4-4.1.jar;poi-3.17/lib/commons-logging-1.2.jar;poi-3.17/lib/junit-4.12.jar;poi-3.17/lib/log4j-1.2.17.jar;poi-3.17/ooxml-lib/curvesapi-1.04.jar;poi-3.17/ooxml-lib/xmlbeans-2.6.0.jar;poi-3.17/poi-3.17.jar;poi-3.17/poi-ooxml-3.17.jar
bin/com/company/Main
Above is what I've been trying to get working so any suggestions on what I might be doing wrong would be appreciated
The whole exception is "Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: com/company/Main (wrong name: Main)"
Note, I'm using windows
#caius-brindescu is right, you have to specify the class like com.company.Main but also, you have to execute the class from inside the bin directory. So, run 'cd bin' and then your command and it should work.
This is like this because your PATH starts with a dot '.' which means here in this directory.
First, the separator for jar files for the -cp argument is the colon (:), not the semicolon (;). Using the semicolon will change how the arguments are parsed.
Also, when you run the main class, you have to specify the fully qualified name (com.company.Main), instead of the path to the class file. The class file will be resolved from the -cp arguments.

How to compile and run project with multiple classes in package?

I just realized I'm about to graduate and I still don't know how to handle this situation:
Say I have a java package named mystuff.project1 and inside the package I have Project1.java (which has the main method) and ThingThatDoesStuff.java . Both are public classes, have the package declaration at the top of the file, etc. I can debug this project fine in Eclipse.
For maximum simplicity, I move the project to C:\ so the java files are located in c:\mystuff\project1
I navigate into c:\mystuff\project1 and type javac *.java to compile the class files
Now how the blazes do I run my application?
java Project1 doesn't work
java mystuff.project1.Project1 doesn't work
java -cp . Project1 doesn't work
java -cp . mystuff.project1.Project1 doesn't work
All of the above give me "Error: Could not find or load main class"
I've been searching SO and elsewhere to try to understand this problem but I'm completely baffled.
I've:
cleared my CLASSPATH variable
triple-checked PATH etc
successfully tried compiling and running standalone class files that aren't in a package
I understand this is a stupid newbie question but I just can't figure it out. I realized every other time I've run into this problem I similarly couldn't find a solution and put all of the classes into a single file. I'd rather learn how to stop doing that now.
I think you have to change to C:\ and then enter:
javac mystuff\project1\*.java
java mystuff\project1\Project1
and this should work.
It's either that or:
javac mystuff/project1/*.java
java mystuff/project1/Project1

Reconstructing an Executable jar (with Modified Class-Path) from Class Files

I'm grading an assignment in Java. Students are asked to implement a Five-In-A-Row (like Tic-Tac-Toe, or two-player Pente) interface which is used by a GUI .java file. These files (interface and GUI) are given to the students in a file called lab2.jar (where they're in cs251/lab2/ under the names GomokuModel and GomokuGUI, respectively), which the students must add to their classpaths. When the project is finished, students are requested to turn in a .java file called Gomoku.java.
One student turned in a .jar, but the command
java -jar Gomoku.jar
responds with
no main manifest attribute, in Gomoku.jar
I figure the student may have forgotten / not known to make a manifest file. I unzip the student's jar and find only .class files. I try to make my own jar from these files:
According to specs, the main must be in Gomoku.java, whose class is Gomoku.class. So I make a manifest.txt file that looks like
Main-Class: Gomoku
Class-Path: lab2.jar
And try to make a .jar out of it using the command
jar cfm myJar.jar manifest.txt *.class lab2.jar
But when I run this using the command
java -jar myJar.jar
I get the following error:
0Exception in thread "main" java.lang.IllegalAccessError: tried to access method cs251.lab2.GomokuGUI.<init>(Lcs251/lab2/GomokuModel;)V from class Gomoku
at Gomoku.main(Gomoku.java:47)
This particular error is giving me trouble. I've never seen anything like it, and my research on the web doesn't turn up anything. Because the error says it's coming from GomokuGUI, which is one of the lab2.jar files, I think the error's on my end. My questions are:
Can I make an executable .jar when I know and have
What goes in the classpath
Where the main should be
A set of relevant class files
If the answer to (1) is yes: Am I going about it in the right way? I have a feeling I'm missing a recompile step somewhere.
In this particular case, I may ask the student to resubmit. And I will download the .jar's I see submitted before due date to make sure they are runnable. But for knowledge's sake (I myself have made .jar files that have had only .class in them and no manifest), is there a way to salvage a working file like the one described above?
From the JRE javadoc:
public class IllegalAccessError
extends IncompatibleClassChangeError
Thrown if an application attempts to access or modify a field, or to
call a method that it does not have access to.
You're getting
0Exception in thread "main" java.lang.IllegalAccessError: tried to access method
cs251.lab2.GomokuGUI.<init>(Lcs251/lab2/GomokuModel;)V from class Gomoku
at Gomoku.main(Gomoku.java:47)
The method it's complaining about is named <init>. That's what Java calls constructors, internally. It's saying that Gomoku.main() tried to issue new GomokuGUI(model) where model is expected to be an instance of GomokuModel, but that this constructor was not accessible. The fact that Gomoku.main() is in a different package from GomokuGUI means the constructor would have to be public for that to work.
You can check that via reflection -- I believe Eclipse can do that for you, actually -- but that's almost certainly what's going on.
So either the student turned in broken code, or you broke it during your attempts to force-fit it into convenience-executable jarfile format. Which was wasted effort in any case, since you can't grade the assignment based on object code and you're going to have to go back and ask for source code anyway.
If you really want to try running the jarfile the student submitted: Go back to the original unmodified jarfile and try just running 'java Gomoku -classpath myJar.jar' where myJar.jar is what the student turned in. If that doesn't work, try 'java Lcs251.lab2.Gomoku -classpath myJar.jar', which is probably the package they intended to put it into given the error message you're getting. If neither of those runs, ask the student what command line they've been using to run it and try that. If THAT doesn't work, then it's time to investigate why.
The whole executable-jar question is a red herring and a waste of time until you know the code actually runs and what the entry point actually is.

running java program from command line with classpath

Most of the time when i am learning something i am running java program from commandline.
now my problem is little complecated.
i am running Simple java program from command line. But i need CLASSPATH variable in environment variables. when i am adding CLASSPATH variable my program is giving me error.
The error is classNOtFoundException. Means when i am running program it will check .class file and it is going in my classpath variable and that is obvious it will not found and it will fire exception.
i am always working line this. this problem occurs after i have formatted my PC. but i am using same OS and same jdk as it was.
Any body have faced this type of issue?
it is not working with following Screen shot.
Working with following screen shot
Following is my desktop screen shot and all things are working with classpath and without classpath(Means i can run simple Program no matter if classpath is there or not.).
Try this,
1. Goto your JDK folder, and then go inside bin,
2. Copy the path
eg: C:\Program Files\Java\jdk1.7.0\bin
3. Paste this path in "PATH", NOT "CLASSPATH" .
4. Then i recommend giving the command "gpupdate /force" in cmd.
(I know it about updating policies, but that how it works for me....)
5. Then type the command "java -version" on cmd, if you get the jdk version in respose, you are good to go.
Try adding "." (current directory) to your system class path.
A better practice would be to create a batch file to set per-application environment variables and launch your program.
Please include the full text of the class not found exception and the system class path you have defined.
According to you your JDK is working properly (because u getting classNOtFoundException)
so defiantly the class which you want to run is not in class path
Lets try this..
java -cp /your/path ClassName
Other wise Lets take a look of this may its helps you.. PATH and CLASSPATH

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.

Categories