| Author |
running java program out of workspace
|
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
I wrote a small java program in my eclipse workspace.
package com.mypackage;
public class HelloWorld {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("Hello, world!");
}
}
}
I am able to compile and run this within eclipse.
Now I open the command window and compile this
C:\Projects\workspace\com\mypackage>javac HelloWorld.java
and it compiled
Now I am trying to run it
C:\Projects\workspace\com\mypackage>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/mypackage/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
this is happening because I have
package com.mypackage;
in the program.
If I delete the line
package com.mypackage;
I am able to compile and run this.
How can I run this without deleting the line
package com.mypackage;
from the program?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
C:\Projects\workspace\com\mypackage>javac HelloWorld.java
and it compiled
Try this instead....
C:\Projects\workspace>javac com\mypackage\HelloWorld.java
Now I am trying to run it
C:\Projects\workspace\com\mypackage>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/mypackage/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
Try this instead....
C:\Projects\workspace>java com.mypackage.HelloWorld
Note: Moving this to JiG (Beginner)
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
Doesn't seem to be working.
C:\Projects\workspace>java com.myPackage.HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: com/myPackage/HelloWorld
|
 |
kannan vinayagam Duraiswamy
Ranch Hand
Joined: Jan 12, 2009
Posts: 52
|
|
try by setting classpath
java -classpath <path for the class file> file name
java -classpath C:\Projects\workspace\com\myPackage\HelloWorld HelloWorld
|
Kannan.DV
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
One possibility:
You may have Eclipse configured to keep source and .class files in different directories. Beware of using IDEs as a beginner; you can spend longer learning about the IDE than about Java.
Copy and paste the HelloWorld.class file into another folder somewhere so it is inside a "mypackage" folder inside a "com" folder.
Then, as Henry said earlier, if you have
C:\---java---com---mypackage---HelloWorld.class
you can try:-
C:\someFolderOrOther> cd C:\java
C:\ java java com.mypackage.HelloWorld
It does take some time to get used to calling classes with a package name.
Another possibility:
If you have mypackage in the source and invoke it with myPackage the capital P will stop the java tool finding the correct location. Remember Java is CaSe-SeNsItIvE.
I don't think setting a classpath will help.
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
If I delete the line
package com.mypackage;
I am able to compile and run this.
As a added statement... by deleting the line, you are using the default package.
|
My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
Campbell Ritchie wrote:Beware of using IDEs as a beginner; you can spend longer learning about the IDE than about Java.
I totally agree to this statement, IDE are the tools to build programs. Dont use them until you have mastered the basics of the language itself
|
 |
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
Actually I am not a beginner. I wrote a job in the folder
C:\Projects\workspace\Project1\src\com\fis\util using eclipse IDE.
This get compiled into the classes folder.
Now I want to run this job from the command line.
I couldn't do it. So I wrote a test program called HelloWorld to test it out.
So I need to run this job from command line.
How do I do that?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Alex George wrote:Doesn't seem to be working.
C:\Projects\workspace>java com.myPackage.HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: com/myPackage/HelloWorld
Java is case sensitive. From you previous posts, you said you package was in "com/mypackage" -- with a small p.
Henry
|
 |
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
|
I tried it. Didnt work.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Please post the directory structure you have, and show us which folder you are in trying to execute your app.
As an alternative: get a directory, empty it, copy and paste your HelloWorld class into it, and compile it with additional tags
javac -d . HelloWorld2.java
The dot tells the compiler to start looking in the "current directory" and -d means to create any directories (if they don't already exist).
Execute with
java com.mypackage.HelloWorld2
with all the spelling and CaPiTaLiSaTiOn correct.
|
 |
Nathan Heimdall
Greenhorn
Joined: Mar 02, 2009
Posts: 20
|
|
Alex,
The way to run your program outside the Eclipse is to create a jar file.
You can use the Fat Jar Eclipse Plug-In and create a jar and then run the jar file from the command line by saying "java -jar filename.jar".
You could supply command line arguments cmd1 and cmd2 if you wanted by saying after the filename - "filename.jar" cmd1 cmd2.
Hope this helps,
|
-Nathan
"A single conversation with a wise man is better than ten years of study."
|
 |
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
my HelloWorld.java file is in
C:\Projects\workspace\Project1\src\com\fis\util
my compiled file is in
C:\Projects\workspace\Project1\classes\com\fis\util
my classpath is
C:\Projects\workspace\Project1\classes\com\fis\util
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Alex George wrote:my HelloWorld.java file is in
C:\Projects\workspace\Project1\src\com\fis\util
my compiled file is in
C:\Projects\workspace\Project1\classes\com\fis\util
my classpath is
C:\Projects\workspace\Project1\classes\com\fis\util
Your HelloWord.java program is in the com.mypackage package. It needs to be in in a com\mypackage directory (from the classpath) to be compile and run properly. In this case, there is no classpath that you can set, that will get it to run correctly..
Henry
|
 |
Alex George
Ranch Hand
Joined: Mar 23, 2007
Posts: 51
|
|
Okay found it.
Go to the classes folder set classpath to classes folder
then
java com.fis.util.HelloWorld.
Thant worked.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Have you set a system classpath? You ought not to need a classpath for that sort of exercise. At least add a . to your system classpath.
|
 |
 |
|
|
subject: running java program out of workspace
|
|
|