Solved

Run Java Class from DOS batch file

Posted on 2013-06-04
7
678 Views
Last Modified: 2013-11-13
I created a Java class from Eclipse and need to deploy it to another machine.

I tried creating a simple batch file to run it like this:

cmd /c java bliffo
pause

Open in new window


However, I get
Exception in thread "main" java.lang.NoClassDefFoundError: bliffo (wrong name: BliffoscopeDataAnalysis/bliffo)

I tried adding JAVA_HOME and PATH and CLASSPATH in various ways but keep getting the same error.

What should my batch file look like (and remember that I have to deploy on someone else's machine with unknown settings, but I'll assume that a jre is installed somewhere on it, such as mine: C:\Program Files\Java\jdk1.7.0_21
0
Comment
Question by:rspahitz
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39221098
Firstly, you will need the following directory structure...

|
|--- run.bat
|
|--- BliffoscopeDataAnalysis     (Note: this is a directory)
                |
                |--- bliffo.class


And then change the batch file (run.bat in the above diagram) to this...

cmd /c java BliffoscopeDataAnalysis.bliffo
pause

Open in new window

0
 
LVL 22

Author Comment

by:rspahitz
ID: 39221147
Yay!  that got me started.

Now I'm getting this:
Exception in thread "main" java.lang.NoClassDefFoundError: BliffoscopeDataAnalys
is/bliffo$TargetType

and my code includes this:

enum TargetType

So I'm guessing this is related...

Any other thoughts?
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 39221159
You need "bliffo$TargetType.class" and any other "bliffo$_______.class" files that Eclipse generated to be copied over to the other machine, not just "bliffo.class" and they go in the same place as "bliffo.class"
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 86

Expert Comment

by:CEHJ
ID: 39221424
You should export the app as an executable jar in Eclipse. That will ensure all dependencies are met and will make the batch file easier to run (see below). Make sure you set the correct main class when you export

cmd /c java -jar bliffo.jar
pause

Open in new window

0
 
LVL 22

Author Comment

by:rspahitz
ID: 39222413
Oddly, I thought those were temp files...that problem gone but now this:

Exception in thread "main" java.lang.NoClassDefFoundError: BliffoscopeDataAnalysis/bliffo$1

on this line:
switch (confidenceLevel)

Open in new window

which comes after
confidenceLevel = getConfidence(hitPct);

Open in new window

which is defined as
private static ConfidenceLevel getConfidence(double percentage)

Open in new window

and
I'll try the jar file idea too.
0
 
LVL 22

Author Closing Comment

by:rspahitz
ID: 39222524
I just realized that when I compiled the code separately, it created the $1 file so I'm ready to go...Thanks!
(I'll investigate the jar file next time.)
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39224392
Not a problem. Yeah, using a jar file will make your deployment MUCH easier.

However, this was probably still a useful exercise for you, so that you understand some of the intricacies of Java class loading and classpaths.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now