Solved

Run Java Class from DOS batch file

Posted on 2013-06-04
7
674 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

747 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

10 Experts available now in Live!

Get 1:1 Help Now