How do you run DDD for Java? - java

I was following this tutorial
here
and got a popup that says "cannot access class test"
after I typed in
%javac -g test.java
%ddd -jdb test &

When using DDD to debug java you must, as well as following the steps in the example you have provided, set the classpath that JDB uses to find your files. Otherwise you will see the message 'Cannot Access Class Test' that you are seeing.
To do this, in DDD click Edit, then JDB Settings, then type '.' (not including quote marks) in the classpath field. This will cause JDB to search in the current directory when looking for your example java code.
For more information about the java classpath and what it does, see
http://en.wikipedia.org/wiki/Classpath_%28Java%29
Hope this helps.
John

%javac -g test.java
%ddd -jdb test &
simple java program that works with these commands:
class test{
static public void Main(String[] args){
System.out.println("hello");
}
}

Related

Java - Could not find or load main class Class01

Using Java 8
I have set my PATH as 'C:\Program Files\Java\jdk1.8.0_144\bin'
I also tried to set my CLASSPATH as 'C:\Program Files\Java\jre1.8.0_144\lib\rt.jar', though I read it is not neccesary.
From Class01.java I have no problem creating Class01.class
javac Class01.java -> created Class01.class
Still, when I try to run program
java Class01
I got message
Error: Could not find or load main class Class01
If anyone know, how to fix this, I appreciate every hint.
Btw. My program does nothing but printing Hello world, if it has something to do with my problem.
You will need to tell Java about the classpath where it should find your file. I think you are missing the classpath parameter in your java command (see below). Here is a simple example how you could create a java file, compile and run it:
A. Create File:
public class X {
public static void main(String[] args) {
System.out.println("Blah!");
}
}
B. Compile:
"%JAVA_HOME%\bin\javac" X.java
C. Run it using the `-classpath` parameter:
"%JAVA_HOME%\bin\java" -classpath . X
This will print out:
Blah!
Note that JAVA_HOME is a System variable in Windows which needs to point to the location of your Java Runtime Environment.

Running Hello Agent in JADE

This maybe is an easy thing to resolve, the thing is that I have hours trying to make it work and nothing. I'm trying to compile and run some agents in JADE, basically are the examples available in the tutorial available in the webpage (I think). So, I have a script file to compile the class and other to run the compiled code, The first one I called it CompileJade.bat, and this is the content:
javac -classpath jade.jar;.\lib\jadeTools.jar;.\lib\iiop.jar;.\lib\base64.jar;. %1 %2 %3 %4 %5 %6 %7 %8 %9
The, the RunJade.bat contains these lines:
java -cp jade.jar jade.Boot -gui
Until here, everything seems to work fine, some compiled files are created now. But then, I 'm trying to run my project but none of the agents appears in the container or the tree in the GUI of Jade, then I tried to run the hello agent example but neither this one appear in the container, I only have the ams, df, and rma agents. This is the content of the hello agent class:
import jade.core.Agent;
public class HelloAgent extends Agent
{
protected void setup()
{
System.out.println("Hello World. ");
System.out.println("My name is "+ getLocalName());
}
}
I typed these lines in the windows terminal:
compileJade HelloAgent.java
RunJade MyAgent:HelloAgent
Then the GUI is initialized but I don't have my agent in the container. So, what's wrong?
The problem may be in in the agent initialization. The statement RunJade MyAgent:HelloAgent needs the package name. The common syntax is <LocalAgentName>:<PackageName>.<ClassName>.
i think you have to add your package in the class.
Alhtough i have another problem regarding this hello agent in jade, the version number 4 does not contain the http.jar, iiop.jar; and base64.jar
Can you please tell me where did you download those files for the 4 version?
Thank you

Could not find or load main class package.class

I know this question has already been answered many times, but unfortunately I couldn't find the right answer to my questions.
below is my package structure and inside my package I have SimpleTest.java
d:\junit\src\junitfaq\SimpleTest.java
inside d:\junit\src> i tried to compile SimpleTest.java and it successfully compiled using the command below.
d:\junit\src>javac junitfaq/SimpleTest.java
but when i try to run the program using command line below
d:\junit\src>java junitfaq.SimpleTest
this error occured. Error: Could not find or load main class junitfaq.SimpleTest
I tried running it by accessing junitfaq package by using this command
d:\junit\src\junitfaq>java -cp . SimpleTest
the program run perfectly. A little help would be much appreciated.
Have you declared your SimpleTest class to be a member of the junitfaq package? If you have, you should be able to run it from the src directory like java junitfaq.SimpleTest but you should get an error like this if you try to run it from within the junitfaq directory: Exception in thread "main" java.lang.NoClassDefFoundError: SimpleTest (wrong name: junitfaq/SimpleTest)
Make sure your SimpleTest class starts with package junitfaq;
Edit: Here's a working example incorporating the comments below.
login#domain:~/temp> mkdir src
login#domain:~/temp> cd src
login#domain:~/temp/src> mkdir junitfaq
login#domain:~/temp/src> nano junitfaq/SimpleTest.java
The contents of SimpleTest.java are as follows when I exit nano:
package junitfaq;
public class SimpleTest {
public static void main(String[] args) {
System.out.println("Test");
}
}
login#domain:~/temp/src> javac junitfaq/SimpleTest.java
login#domain:~/temp/src> java junitfaq.SimpleTest
Test
It sounds like you have a classpath problem; you should double-check the location of your class file, the directory/package structure, the location from which you're trying to run the java command, and any classpath specified during execution.
For example, the following works for me:
$ mkdir junitfaq
$ cat >junitfaq/SimpleTest.java
package junitfaq;
public class SimpleTest
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
^D
$ javac junitfaq/SimpleTest.java
$ java junitfaq.SimpleTest
Hello, world!
$ java -cp . junitfaq.SimpleTest
Hello, world!
$
Not to belabor the obvious, but I noticed a spelling typo in one of your comments - you should also double-check that you're running the command as intended.
trialYou could try putting your java and class files together with your libaray folder (if any) in a new file called "trial" in your C:\ directory then compile existing java file using the following->
C:\trial> javac -cp .;library folder* SimpleTest.java
(then)
C:\trial> java -cp .;library folder* SimpleTest
Let me know how you get on!

Unable to run HelloWorld from command line

OK, I just cannot get java to run my .class files:
I follow steps in Oracle tutorial and try to run this program:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Compiling OK:
PS C:\Users\Ztaz> javac .\HelloWorldApp.java
But after I try to run it, I get this:
PS C:\Users\Ztaz> java .\HelloWorldApp.class
Error: Could not find or load main class .\HelloWorldApp.class
no exception, nothing.
Here's my PATH variable, if it helps (split into lines, for readability):
%JBOSS_HOME%;
%SYSTEMROOT%;
%M2%;
%JAVA_HOME%\bin;
...
JAVA_HOME is set to "C:\Program Files\Java\jdk1.7.0". My question sounds a lot like this one but I had this problem on Java SE 6 as well, so I decided to post separate question.
Run it without the .class: java HelloWorldApp
This causes issues for lots of people starting out with Java. Not sure why Java doesn't just look for both files (the name provided and the name with .class appended).

javac error: Class names are only accepted if annotation processing is explicitly requested

I get this error when I compile my java program:
error: Class names, 'EnumDevices', are only accepted if annotation
processing is explicitly requested
1 error
Here is the java code (I'm running this on Ubuntu).
import jcuda.CUDA;
import jcuda.driver.CUdevprop;
import jcuda.driver.types.CUdevice;
public class EnumDevices {
public static void main(String args[]) {
CUDA cuda = new CUDA(true);
int count = cuda.getDeviceCount();
System.out.println("Total number of devices: " + count);
for (int i = 0; i < count; i++) {
CUdevice dev = cuda.getDevice(i);
String name = cuda.getDeviceName(dev);
System.out.println("Name: " + name);
int version[] = cuda.getDeviceComputeCapability(dev);
System.out.println("Version: " +
String.format("%d.%d", version[0], version[1]));
CUdevprop prop = cuda.getDeviceProperties(dev);
System.out.println("Clock rate: " + prop.clockRate + " MHz");
System.out.println("Threads per block: " + prop.maxThreadsPerBlock);
}
}
}
Here is the javac command:
javac -cp /home/manish.yadav/Desktop/JCuda-All-0.3.2-bin-linux-x86_64 EnumDevices
How do I compile this program?
You at least need to add the .java extension to the file name in this line:
javac -cp /home/manish.yadav/Desktop/JCuda-All-0.3.2-bin-linux-x86_64 EnumDevices
From the official faq:
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.
Also, in your second javac-example, (in which you actually included .java) you need to include the all required .jar-files needed for compilation.
I was stumped by this too because I was including the .Java extension ... then I noticed the capital J.
This will also cause the "annotation processing" error:
javac myclass.Java
Instead, it should be:
javac myclass.java
Using javac ClassName.java to compile the program,
then use java ClassName to execute the compiled code. You can't mix javac with the ClassName only (without the java extension).
The error "Class names are only accepted if annotation processing is explicitly requested" can be caused by one or more of the following:
Not using the .java extension for your java file when compiling.
Improper capitalization of the .java extension (i.e. .Java) when compiling.
Any other typo in the .java extension when compiling.
When compiling and running at the same time, forgetting to use '&&' to concatenate the two commands (i.e. javac Hangman.java java Hangman). It took me like 30 minutes to figure this out, which I noticed by running the compilation and the running the program separately, which of course worked perfectly fine.
This may not be the complete list of causes to this error, but these are the causes that I am aware of so far.
I learned that you also can get this error by storing the source file in a folder named Java
chandan#cmaster:~/More$ javac New.java
chandan#cmaster:~/More$ javac New
error: Class names, 'New', are only accepted if annotation processing is explicitly requested
1 error
So if you by mistake after compiling again use javac for running a program.
How you can reproduce this cryptic error on the Ubuntu terminal:
Put this in a file called Main.java:
public Main{
public static void main(String[] args){
System.out.println("ok");
}
}
Then compile it like this:
user#defiant /home/user $ javac Main
error: Class names, 'Main', are only accepted if
annotation processing is explicitly requested
1 error
It's because you didn't specify .java at the end of Main.
Do it like this, and it works:
user#defiant /home/user $ javac Main.java
user#defiant /home/user $
Slap your forehead now and grumble that the error message is so cryptic.
Perhaps you may be compiling with file name instead of method name....Check once I too made the same mistake but I corrected it quickly .....#happy Coding
first download jdk from https://www.oracle.com/technetwork/java/javase/downloads/index.html.
Then in search write Edit the System environment variables
In open window i push bottom called Environment Variables
Then in System variables enter image description here
Push bottom new
In field new variables write "Path"
In field new value Write directory in folder bin in jdk like
"C:\Program Files\Java\jdk1.8.0_191\bin"
but in my OS work only this "C:\Program Files\Java\jdk1.8.0_191\bin\javac.exe"
enter image description here
press ok 3 times
Start Cmd.
I push bottom windows + R.
Then write cmd.
In cmd write "cd (your directory with code )" looks like C:\Users\user\IdeaProjects\app\src.
Then write "javac (name of your main class for your program).java" looks like blabla.java
and javac create byte code like (name of your main class).class in your directory.
last write in cmd "java (name of your main class)" and my program start work
To avoid this error, you should use javac command with .java extension.
Javac DescendingOrder.java <- this work perfectly.
I created a jar file from a Maven project
(by write mvn package or mvn install )
after that i open the cmd , move to the jar direction and then
to run this code the
java -cp FILENAME.jar package.Java-Main-File-Name-Class
Edited : after puting in Pom file declar the main to run the code :
java -jar FILENAME.JAR
If you compile multiple files in the same line, ensure that you use javac only once and not for every class file.
Incorrect:
Correct:

Categories

Resources