This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java commandline doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java commandline doesn Watch "java commandline doesn New topic
Author

java commandline doesn't work

christine clarin
Ranch Hand

Joined: Feb 05, 2005
Posts: 106
hello, i already made javac work in compiling my java codes. i had two classpaths set: one for my packages and one for my jar file. this is what i used:

javac -cp <path1>:<path2> codetocompile.java

and it worked. the codetocompile.java already has the main code that i want. it compiles as well i.e., it releases a .class file. however, when i run it with

java codetocompile

it tells me that i have no main in that code and noclassdeffound exception is thrown. i can't run my program!how do i solve this? thanks! i really need some help =(
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

1: "it tells me that i have no main in that code..."

Do you have a 'main' method in your class that has the following signature:

public static void main(String[] args)

Note: it must be public, static, void, the name must be 'main' and it must have a String array as a parameter.

2: "...and noclassdeffound exception is thrown"

You have to use the "-cp ..." also when you run the program, not just when you compile the program. Make sure the directory that contains the class file is included in the classpath.
[ April 19, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
christine clarin
Ranch Hand

Joined: Feb 05, 2005
Posts: 106
hello, yes I have public static void main (String args[])present in my codetocompile java...i also already used this:
java -cp path1 : path2 codetocompile

but it still gives me this:
Exception in thread "main" java.lang.NoClassDefFoundError: InfectedCellCount

why is this so? thank you!
[ April 19, 2007: Message edited by: christine clarin ]
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I might be totally wrong, and I can't test it right at the moment, but I've seen this happen when you type in the file extension at the command line. Like, if you type "java codeToRun.class" it gives you that error, but if you just type "java codeToRun" it should work fine... I think that's what happened in my case.


Studying for SCJP 6
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by christine clarin:
i also already used this:
java -cp path1 : path2 codetocompile


Unless you have enclosed the whole thing in quotes, there should be no spaces in the classpath.
i.e. it should be
java -cp path1:path2 codetocompile


If it still doesn't work when you've removed the spaces, then post the actual command line you are using and tell us where the InfectedCellCount class is.

[ April 19, 2007: Message edited by: Joanne Neal ]
[ April 19, 2007: Message edited by: Joanne Neal ]

Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java commandline doesn't work
 
Similar Threads
Web Service with build.xml
Compile and run a java program thru ant
running jar file error:
Problem with logic and bean tag
Execute my java program with Ant