Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running java program out of workspace

 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try by setting classpath

java -classpath <path for the class file> file name

java -classpath C:\Projects\workspace\com\myPackage\HelloWorld HelloWorld

 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it. Didnt work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Alex George
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay found it.
Go to the classes folder set classpath to classes folder
then
java com.fis.util.HelloWorld.

Thant worked.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic