Running Hello Agent in JADE - java

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

Related

Could not load main method when setting class path

I am trying to run a java program in linux server which includes an imported jar.
While compiling it is not giving any issues , but when I try to run, it is giving an error "Error: Could not find or load main class ". If I remove the import and its references , my code is working fine without any load issues.
Any inputs please
javac -cp "/opt/CARKaim/sdk/pwdsdk.jar" SamplePwd.java //No issues
java -cp "/opt/CARKaim/sdk/pwdsdk.jar" SamplePwd //Error: Could not find or load main class
Assuming SamplePwd has an entry-point (e.g. main(String[] args)) make sure the current folder is also in your class-path, like
java -cp "/opt/CARKaim/sdk/pwdsdk.jar:." SamplePwd

Calling Java from Python: "Can't find or load class" Error

I'm trying to call a java program from python using command line. The code is as follows:
subprocess.check_output(["java", "pitt.search.semanticvectors.CompareTerms", "-queryvectorfile","termvectors.bin","term1","term2"])
I get the following error:
Error: Could not find or load main class pitt.search.semanticvectors.CompareTerms
This happens when I run the program from PyDev (version 2.5 in Eclipse 3.7.2). However, if I run the same code from the terminal, it works and I get the result I want.
I'm almost sure that the problem is related with some configuration of PyDev and how it handles the java CLASSPATH, which is:
/Users/feralvam/Programas/semanticvectors-3.4/semanticvectors-3.4.jar:/Users/feralvam/Programas/lucene-3.5.0/lucene-core-3.5.0.jar:/Users/feralvam/Programas/lucene-3.5.0/contrib/demo/lucene-demo-3.5.0.jar:
The class "pitt.search.semanticvectors.CompareTerms" is in "semanticvectors-3.4.jar".
Any help you could give me would be really appreciated.
Thanks!
The solution proposed by #eis worked. Now, the command is:
subprocess.check_output(["java", "-classpath", "/Users/feralvam/Programas/semanticvectors-3.4/semanticvectors-3.4.jar:/Users/feralvam/Programas/lucene-3.5.0/lucene-core-3.5.0.jar:/Users/feralvam/Programas/lucene-3.5.0/contrib/demo/lucene-demo-3.5.0.jar:", "pitt.search.semanticvectors.CompareTerms", "-queryvectorfile","/Users/feralvam/termvectors.bin","term1","term2"])

Java virtual machine launch issue

HI All,
I got an issue, all of a sudden Java stopped working completely. I start getting error like "Could not create the virtual machine". There is no issue with the memory (it has 3GB RAM) and was working fine for over a 6 months in this system without any issue.
Here are some peculiar behaviors -
When I start eclipse I see Java virtual machine dialog box with error messages like
"Could not find main class org.eclipse......support.legacysystemproperties"
Eclipse is able to start(with above error), but while running the program, I get error like "Could not create Java Virtual Machine" in a dialog box and after I click OK on that dialog box, I see error like "unrecognized option -dfile.encoding=cp1252
I used text editor, wrote a class Test.java (without any package), compiled it (Edit #1:javac Test.java). But when I execute the program (Edit #1:java Test), I get the following error -
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: Test).
Edit #1:
Note : The compiled file, Test.class is successfully created in the directory. I did recheck the path and classpath environment variables. All seem to be correct.
Please note that there seems to be some issues with cases which affected the Java.
I did uninstall Java (all versions), re-installed, but nothing helped. Also, I did run CCleaner to clean registry, Malwarebytes' Anti-Malware, but none helped so far.
Appreciate if someone could help me to resolve the issue.
I did googled for this and found that some have experienced similar issues, but none of them have found solution yet other than some suggestion that re-installation of Windows OS itself, which I want to avoid it. I did system restore, but that failed for some other
reason.
Please note that am using Java for over 10 years. This is first time am having such issue. This is something to do with Windows Registry or some other system configuration, but I am not able to find out the exact problem.
Anyways awaiting some good suggestion.
EDIT: Okay, so it looks like the Java executable is getting the command line arguments lower-cased.
Step 1: Verify
You can double-check whether this affects all command line arguments by creating a class with a lower-case name which just dumps its arguments:
public class test {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
Compile and run this with a variety of inputs.
Step 2: Check scope
Assuming step 1 confirms the problem, if you've got .NET installed you can see whether it affects that as well. Create a file Test.cs:
using System;
class Test
{
static void Main(string[] args)
{
foreach (string arg in args)
{
Console.WriteLine(arg);
}
}
}
Compile this with "csc Test.cs" having found csc in the .NET framework directory (e.g. in c:\Windows\Microsoft.NET\Framework\v4.0.30319 for .NET 4).
Run it like this:
Test foo BAR Baz
and see what happens
Step 3: If step 2 showed that the issue is limited to java.exe:
Check your path, and work out where you're actually running java.exe from
Try explicitly running java.exe from your JRE or JDK directory

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:

How can I execute a Java program within a php script?

I am writing a simple web upload script.
The goal is to upload a file using php, and then calling a java program to process this file.
I have done the work for uploading the file, but I cannot get a java program to be successfully run from within the php script.
I have tried exec(), shell_exec(), and system() with no results.
For the command, I have used "java Test", "java < directory >/Test", "/usr/bin/java < directory >/Test", I have even set up the application as a jar file with no results. The actual line of code I have used is:
echo shell_exec("java Test");
Usually there is no output. However, if I have just shell_exec("java"), then the last line of the help from java ("show splash screen with specified image") is displayed, which shows that the command has been executed. If I use, for example, shell_exec("whoami") I get "nobody" returned, which is correct. The only thing the java file does is create a file so that I can see that the application has been successfully run (the application runs successfully if I run it on the command line). I have set the permissions for the java file to 777 to rule out any possibility of permission errors. I have been struggling with this for a while trying all sorts of options with no results - the file is never created (the file is created with an absolute path so it's not being created and I just can't find the file). Does anyone have any ideas?
Thanks.
I have been struggling with this for a
while trying all sorts of options with
no results - the file is never created
(the file is created with an absolute
path so it's not being created and I
just can't find the file). Does anyone
have any ideas?
What I think the problem is. Apache runs as "nobody" group??(apache user??) which will execute the java script which will try to create a file on disc somewhere. I assume it does not have permission to write to that location. you should chown that folder so that apache user can write to that folder.
==
First off I would like to point out to you that calling exec() from a script could really blow up your server. I would advice you to use something like redis(see below) instead.
==
Second I think I know what the problem is. You should first try to run the simple example below which worked fine for me.
==
First be sure permission are set right. Because apache runs as nobody(most of the times).
I tried this simple test myself on ubuntu with php installed from repo.
test.java
class test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
test.php
echo exec('java test');
Ran test.php
$ php test.php
Hello World!
==
Or you could try 1 of the following solutions(which would even be a better solution):
Write your java program as a webservice for example on top of atmosphere-spade-server(simple/embedded jar). This could be written insanely fast. But on high load this will not be best option I guess. Still I think this will be more than fast enough for you probably. Even this way it will be much faster as executing it, because you won't have the overhead running JVM. Could blow up your server, not as fast as exec()
Do a blocking pop/push from a redis(*nix) list structure. This will be pretty easy to write on *nux because there are client libraries for both java/php. The speed will best I guess because redis is written in C. I use redis myself.
Use a JMS like for example activemq. Also pretty easy to write because good library support. I have not used a JMS myself. I use redis solution. The speed I guess would be a little less then with redis solution.
I dont realy know, but i came a cross PHP-JAVA bridge maybe it can help
http://php-java-bridge.sourceforge.net/pjb/
Update:
I tested this with Jasper Reports, and it is working really nice. It will allow you to Extend Java classes with PHP or just use Java class lik it was PHP.
use java\lang\String as JString;
require_once("javabridge/java/Java.inc");
class String extends JString {
function toString () {
return "hello " . parent::toString();
}
}
$str = new String("Java");
echo $str->toString();
or
$temp = new Java('java.sql.Timestamp');
$javaObject = $temp->valueOf('2007-12-31 0:0:0');
$params = new Java("java.util.HashMap");
$params->put("text", "This is a test string");
$params->put("date",$javaObject);
More examples: http://php-java-bridge.sourceforge.net/pjb/FAQ.html
It's possible it has to do with the path that the exec is defaulting to. You may need to explicitly define your classpath with an absolute path to your .class or jar files when calling java.
<?php
$PATH="C:\Program Files\Java\jdk1.7.0_09\bin";
echo exec("javac theNameOfYourJavaProgram.java 2>&1");//shows # of errors
echo "<br />";
echo exec("java theNameOfYourJavaProgram 2>&1");//this line executes it
echo "<br />";
echo shell_exec("javac theNameOfYourJavaProgram.java 2>&1 ");//compiles it
?>

Categories

Resources