class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hey");
}
}
Command prompt session:
C:\Users\zobdos\Desktop>javac HelloWorld.java
C:\Users\zobdos\Desktop>dir *.class
Volume in drive C is OS
Volume Serial Number is A45E-7B01
Directory of C:\Users\zobdos\Desktop
11/20/2010 10:16 AM 417 HelloWorld.class
1 File(s) 417 bytes
0 Dir(s) 8,145,432,576 bytes free
C:\Users\zobdos\Desktop>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: HelloWorld. Program will exit.
C:\Users\btolbert\Desktop>
Nevermind, it works after using:
java -classpath . HelloWorld
You've a %CLASSPATH% environment variable in your environment. Get rid of it, it's disturbing your java commands, it's a poor practice tought by Sun Oracle anyway. Once you use -classpath argument or its -cp shorthand, then the %CLASSPATH% will be overridden. If the classpath is not specified by either the environment variable or the arguments, then the current path will be taken as default (as you initially expected).
run with classpath specified to the current directory:
java -cp . HelloWorld
Related
I used to launch a java tool with a batch like this :
java -classpath "./lib/JSanExport.jar;./lib/JSanRmiApiEx.jar;./lib/JSanRmiServerUx.jar" -Xms128M -Xmx768M -Dmd.command=command_VSPLA.txt -Dmd.logpath=log sanproject.getmondat.RJMdMain
For some reasons i need to convert that batch into a Powershell script. But when I copy the same line on my powershell script I got an error, whereas I launch it in the same workingdir.
c:\Program Files\ExportTool\export>powershell .\Run_VSPLA.ps1
Exception in thread "main" java.lang.NoClassDefFoundError: /command=command_VSPLA/txt
Caused by: java.lang.ClassNotFoundException: .command=command_VSPLA.txt
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: .command=command_VSPLA.txt. Program will exit.
Do you know what happens?
try to set jvm options before classpath :
java -Xms128M -Xmx768M -Dmd.command=command_VSPLA.txt -Dmd.logpath=log -classpath "./lib/JSanExport.jar;./lib/JSanRmiApiEx.jar;./lib/JSanRmiServerUx.jar" sanproject.getmondat.RJMdMain
It searches for class named /command=command_VSPLA/txt. For batch = is a delimiter and is parsed in a different way than powershell.
EDIT
The solution given by the OP:
java -Xms128M -Xmx768M "-Dmd.command=.\command_VSPLA.txt" "-Dmd.logpath=log" -classpath "./lib/JSanExport.jar;./lib/JSanRmiApiEx.jar;./lib/JSanRmiServerUx.jar" sanproject.getmondat.RJMdMain
Seem like it can't find command_VSPLA.txt, maybe try a full path.
I am trying to execute next:
C:\dev\sources\boSchedules\loadJavaLibs>java -cp aurora.jar; ojdbc6.jar
oracle.aurora.server.tools.loadjava.LoadJavaMain -thin -user login/pass#myserv:mysid
%BOS_SRC%/credit/card/api/ScheduleCardApi
And I get next:
Exception in thread "main" java.lang.NoClassDefFoundError: ojdbc6/jar
Caused by: java.lang.ClassNotFoundException: ojdbc6.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: ojdbc6.jar. Program will exit.
Why am I getting this error?
Take a look at the Java Application Launcher man page.
java -cp aurora.jar; ojdbc6.jar
oracle.aurora.server.tools.loadjava.LoadJavaMain -thin -user sched/sched#teach:prod
%BOS_SRC%/credit/card/api/ScheduleCardApi
You have a space between your classpath entries aurora.jar; ojdbc6.jar. The launcher thinks the first jar is the only classpath entry and the ojdbc6.jar is your class containing the main(String[] args) method. It also considers everything after that as arguments to pass to the main(String[] args) method. Remove that space.
Remove the space between the aurora.jar; and the ojdbc6.jar
Spaces delimit the parameters each other. The JVM interprets the command as if you run "ojdbc6.jar" class: "jar" as a classname and "ojdbc6" as a package.
To concat the names of libraries you want to place into classpath for a specific class run please use semicolon with no spaces as "lib1;lib2"
P.S.: Could you please ask your colleagues if you may paste some of our credentials to the SO? :)
i'm trying to run testng tests using command line like this :
C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>java -cp
C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\
testng-6.3.1.jar org.testng.TestNG testng.xml
but i'm getting this error :
Exception in thread "main" java.lang.NoClassDefFoundError: and
Caused by: java.lang.ClassNotFoundException: and
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: and. Program will exit.
So How to add testNG to the classpath.
Thanks
The spaces in the filename are confusing things - Java thinks that only the c:\Documents part is in the classpath argument. Try this:
java -cp
"C:\Documents and Settings"\Administrateur\.m2\repository\org\
testng\testng\6.3.1\testng-6.3.1.jar org.testng.TestNG testng.xml
(where the whole thing is on one line and there's no break after "org" of course)
If you want to run via java on command line, you'll need to either enquote your path ("C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\") or alter slightly as follows: C:\Documents%20and%20Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\. You may also want to run via maven to avoid having to specify this classpath info.
I installed the latest version of Weka on Windows
http://www.cs.waikato.ac.nz/ml/weka/
and I tried to run one of the weka features from the command line:
java weka.core.converters.TextDirectoryLoader -dir text_example > text_example.arff
which is a reference to this: http://weka.wikispaces.com/Text+categorization+with+WEKA
but then weka returns this error:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Exception in thread "main" java.lang.NoClassDefFoundError: weka/core/converters/
TextDirectoryLoader
Caused by: java.lang.ClassNotFoundException: weka.core.converters.TextDirectoryL
oader
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: weka.core.converters.TextDirectoryLoader. Progra
m will exit.
I already set the CLASSPATH environment variable to point to the weka jar.
What did I do wrong?
The shell you run the command in apparently doesn't have the CLASSPATH environment variable set properly, otherwise the ClassNotFoundException wouldn't occur. As an alternative to the environment variable you can specify the classpath using the command line:
java -cp path/to/weka.jar weka.core.converters.TextDirectoryLoader -dir [...]
You can also check whether the environment variable is set correctly by executing echo %CLASSPATH% in the cmd shell.
I'm trying to run one of the very first examples from the book "Head First Java";
public class MyFirstApp {
public static void main (String[] args){
System.out.println("I Rule!");
System.out.println("The Worlds!");
}
}
"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result):
C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class
Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: \hfj\MyFirstApp.class. Program will exit.
You need to run it as
javac MyFirstApp.java
java MyFirstApp
From the directory where MyFistApp.java lives.
'javac' calls the compiler - to that you need to pass your .java file.
'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp"
Specifying the full path of the file should work when you are not in that directory. But are you in the directory that holds the javac and java programs? If not, those may also need absolute paths if you have not put them on your PATH variable.
What ts the package name? Maybe you have something like
package org.test;
in your header?