This is my first post on the forum, hope all of you guys are well.
I've got a issue using JiST/SWANS, the ad hoc simulator in java within eclipse.
I managed to load the API, (as an external JAR ofcourse) but Im basically having a problem integrating the runtime of JiST within eclipse.
After running the hello world im usually getting a stackoverflowerror exception, since it may need modifications within the runtime.
import jist.runtime.JistAPI;
public class hello implements JistAPI.Entity {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("simulation start");
hello t = new hello();
t.myEvent();
}
public void myEvent()
{
JistAPI.sleep(1);
myEvent();
System.out.println("hello world, t=" + JistAPI.getTime());
}
}
the website is: http://jist.ece.cornell.edu/index.html
Thank you in advance!
Actually you need to run Main.java within jist.runtime. But before rigth click Main.java, properties, Run/Debug settings, New,Arguments and type your class name (plain name no .java needed) in Progam arguments. This will tell the jist interface to translate your code using the rewriter and run it.
Examples:
To run hello.java type "hello"
To run aodvsim.java type: "jist.swans.Main driver.aodvsim"
If there are arguments needed type them after the clas name like: "jist.swans.Main driver.aodvsim -n 25 -f 2000x2000 -a grid:5x5 -t 10,600,60"
Wilmer Arellano
How well does SWANS work? Given that the documentation and code date back to 2005, I am not sure if this is the best platform to use.
Related
I am coding java on vscode. I have source folder:
>...
>lib
>>src
exam1.java
exam1.class
>>Month10
app1.class
app1.java
With:
exam1.java
public class exam1 {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
app1.java
package Month10;
public class app1 {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
I want to ask about why I need to use package Month10 in here. And another, exam1.java run well(both run code and run java), but in app1.java, I only can "run java", can't "run code" (Ctrl+Alt+N in vs code), it exists error Could not find or load main class app1, I think because the command package? (I saved file before running), can anyone help me, thanks.
The Code Runner extension sometimes has problems running code under multiple levels of folders. For a better coding experience, please use the official extension to run the code.
Tips:
Run Code is provided by Code Runner
Run Java and Debug Java are provided by the official extension
You can ask questions about the Code Runne here.
reload VS Code window or F1 ->Clean the java language server workspace. try this could help
I am relatively new to programming and java and am trying to learn how to create a user-defined package from the command-line. I get the following: Error: Could not find or load main class TestPhone. I've reviewed posts on this type of error including the well-commented post here. The post lists 4 steps that the JVM goes through to run a java program:
Search for the compiled version of the class.
Load the class.
Check that the class has a main method with signature static void main(String[]).
Call that method passing it the command line arguments as a String[].
Apparently, my JVM can't find my TestPhone.class for some reason I am yet to figure out. Here's my directory structure:
My classpath is set as follows:
My classes contain simple codes from Mala Gupta to test accessibility of class variables:
package mobile;
class Phone {
static boolean softKeyboard = true;
}
package mobile;
class TestPhone {
public static void main (String[] args) {
Phone.softKeyboard = false;
Phone p1 = new Phone();
Phone p2 = new Phone();
System.out.println(p1.softKeyboard);
System.out.println(p2.softKeyboard);
p1.softKeyboard = true;
System.out.println(p1.softKeyboard);
System.out.println(p2.softKeyboard);
}
}
Any idea why it doesn't find my classes? Many thanks.
System specs:
Java version 1.8.0
Javac 1.8.0
Win 7 on 32-bit OS
The java application launcher, java, expects the fully qualified name of the class, mobile.TestPhone. The ../mobile directory need not be in the classpath.
You are in the myJavaProject folder in cmd.
Try to go to c:\myJavaProject\mobile then it should work because your class is in mobile not in
myJavaProject folder
Trying to learn Java again and I cannot remember how I figured this out the first time around.
I have 3 classes; a GameLauncher, GuessGame, and Player. GameLauncher has my main method.
They are all packaged as chap02, I cannot remember if that is important to me yet.
I am compiling like this: javac GameLauncher.java GuessGame.java Player.java
running like this: java GameLauncher
I am getting this error: Could not find or load main class GameLauncher.
I know this is a ridiculous issue, but I have always had trouble with this kind of stuff. The actual programming and writing code I can pick up just fine, but dealing with these damn compilers always gets me. Any help would be appreciated. Thanks
package chap02;
public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
Edit: The issue isn't with the actual code, the issue is with how I am compiling it.
When running the program, you have to run it form the correct directory. Remember, that you used packages, so you have the following package-structure
src
chap02
GameLauncher.java
GuessGame.java
Player.java
after compilation, you will find the corresponding .class-files in the chap02-folder. To start the game, you have run the following command from the src-directory:
java chap02.GameLauncher
Since you specified a package in the source code, you have to use the full qualified name, including the package, to which the class belongs.
EDIT as vefthym mentioned, you have to compile the code in the same way, running
javac chap02/GameLauncher.java
from the source-directory.
EDIT 2
"src" is the directory, where your src lies. I, for example, have my Code unter X:\JDK-Projects[Project-name]\src. You have to specify the full absolute path to the src-Directory or the relative path form the directory you are currently in.
My DrJava was working fine, but now I keep getting the folowing error whenever I run anything:
Static Error: This class does not have a static void main method accepting String[].
So it will compile OK, but then it shoots out the error . This happens even though everything I test does indeed have a public static void main(String[] args) in it. It seems like a classpath/resources type of error. I appreciate any tips
EDIT: my class
public class Test{
public static void main(String[] args){
System.out.println(" hashmap ");
}
}
There's nothing wrong with the code, so the problem must be with the environment.
Check that you're actually executing that class. Find out where the class that's executed is specified and check it's correct
Check that you're compiling the class. Maybe the code you're looking at has not been compiled and you're trying to execute an old version that was compild before you coded a main()
Check your classpath. Is the compiled class accessible in the classpath of the java command
You don't need to reinstall java, nor is it a java version issue. It may be the way that your are running the program.
To check if it is a problem with your code, do the following:
Make a new folder and put Test.java in it.
Open up Command Line Or Terminal and change to that folder .
Type javac Test.java. Test.class should be in the folder now.
If you want, open up the class with a text editor. This is what I get:
˛∫æ2
<init>()VCodeLineNumberTablemain([Ljava/lang/String;)V
SourceFile Test.java hashmap Testjava/lang/Objectjava/lang/SystemoutLjava/io/PrintStream;java/io/PrintStreamprintln(Ljava/l ang/String;)V! *∑±
% ≤∂±
Back to the command line or terminal, type java Test.
If you get an error, which you shouldn't, I don't know what to say. It should produce the string " hashmap " on to the command line or terminal.
Why re-installing Dr. Java may not work is because you may be using the same working directory, causing same run settings to be used. Dr. Java may be running an external program, one without a main method.
I think that you should install the Eclipse IDE for Java. It is much easier to get around, it looks nicer, and it runs the file or project that you are looking at currently.
Sometimes this problem happens because may be mistake in saving file.you always your file using double quotes and with the .java extension which is main class means that class containing main method.
you should save your file by class name which is public .if there is two classes and both have main method then you should save your file by class name that is public and that class will be run.As like your compiler looking for main method in public static void main(String [] args) that is contract for jvm to run a programme
so it is not able to found that main method that is static and it looking for your Dr class.java
See this Example it have two main methods and practice these kinds of question.I also got this kind of problem in starting.
public class TestFirst
{
public static void main(String [] args){
System.out.println(" TestFirst ");
}
}
class Test{
public static void main(String [] args){
System.out.println(" hashmap ");
}
}
if you save pro-gramme by "TestFirst.java" then o/p will come TestFirst if you do some mistake in main method because we have saved our programme by TestFirst then you will get error like you got.
# 2nd mistake may be this
debian#debian:~/Geany_java$ javac Test1.java
debian#debian:~/Geany_java$ java Test1
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at Test1.main(Test1.java:11)
your classpath has not set properly See above Compiling successfully but running showing same kind of error you got.Which OS is using I can guide you properly.
Check that actually your file have the .java termination nor the .dj
There is nothing wrong with the code.
It is the executing environment which might have problem. Please share the details.
Check if program compiled correctly.
Check time-stamo of .class file.
Check permissions on folder/directory where class-files are getting generated.
Check if DrJAVA has appropriate permission on the directory.
Did you create a file, compiled it with out main?
Check class-path. Might be possible that previous class file is still being found by JDK in classpath.
Try compiling .java file from cmdLine instead of editor.
As others have mentioned, your code is fine. There must be a problem with your environment. I recently experienced a similar issue when investigating and answering this question.
Basically, in that question, the code Void.class instanceof Class resulted in a compiler error because a user-made Class.class existed in the classpath, so one Class (the Java built-in java.lang.Class) didn't match with the given Class (user-made).
Something similar may be at work here. It is possible that there is a user-made String.class in your classpath. Then in your main signature, String[] args would mean an array of your String, when Dr. Java must be looking for a main method taking an array of the Java built-in String, i.e. java.lang.String[]. If you have a custom String class in your classpath (or in your project?), then the Java compiler will choose it over the built-in String. If you were to compile and run your Test class from the command line, then you would get the runtime error: Exception in thread "main" java.lang.NoSuchMethodError: main.
Following #S0urceC0ded's suggestion, you may find this when looking at Test.class in a text editor:
main([LString;)V // A user-made String class
instead of what it's supposed to be:
main([Ljava/lang/String;)V // The built-in java.lang.String class
If so, remove your own String class (at least the .class file, but also the .java file so the .class file isn't re-created) from the classpath, and compile and run your Test class again.
Without a look at your environment, I can't tell for sure that this is the issue. But it can explain it.
If you are using Dr.Java as IDE, then you need to make sure that the main class containing 'public static void main' should be at the very top of your program. Otherwise Dr.Java throws this error during runtime.
I have an error in my first step with Java, so when i try to run the code hello world:
class apples{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
I go to: - Run as .. -> Then i choose Java aplicacion - > And i press Ok
But when i press Ok does not appear the window down to show me the correct message Hello World
Your code works fine for me:
class apples
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
I downloaded it to c:\temp\apples.java.
Here's how I compiled and ran it:
C:\temp>javac -cp . apples.java
C:\temp>dir apples
Volume in drive C is HP_PAVILION
Volume Serial Number is 0200-EE0C
Directory of C:\temp
C:\temp>dir ap*
Volume in drive C is HP_PAVILION
Volume Serial Number is 0200-EE0C
Directory of C:\temp
08/15/2010 09:15 PM 418 apples.class
08/15/2010 09:15 PM 123 apples.java
2 File(s) 541 bytes
0 Dir(s) 107,868,696,576 bytes free
C:\temp>java -cp . apples
Hello World!
C:\temp>
Your lack of understanding and the IDE appear to be impeding your progress. Do simple things without the IDE for a while until you get the hang of it. A command shell and a text editor will be sufficient.
Sorry about missing javac; cut & paste error.
If you look at the screenshot, your class name is there, last in the list. Select it and press OK. To not see this message again, right-click on the class name on the left side and select there Run...->Java Application.
The only problem that causes your error here is that the classname and the filename do not match - and they have to.
Solution
Rename either the file thesame.java to apple.java or the class to thesame. Then if you select "Run as..." again, eclipse will present a menu item to start your Java application.
(other mentioned, that there's no requirement that a top-level class and the filename do match - unless the top level class is public. Of course this is true. But the problem was about "running" a class under eclipse as a Java application)
Try public class apples and make sure the file is apples.java. Also it should be public static void main(String[] args)
You have 2 classes by name of "thesame.java" under the source folder. Since one is directly under the src folder, and other under (default package), they use the same namespace, hence Interpreter is confused which java file to execute and is asking you to select the class you want to execute.
Class names must be capitalized... so change apples to Apples. Also, if you are a beginner (which it seems like), I would recommend the Netbeans IDE -- it's a bit more friendlier for new users than Eclipse.
You class must be named "thesame" if you store it in a file called "thesame.java", as you have. Either rename your class to "thesame" or change the file to be "apples.java".
You should move the "[]" to be before "args". So, String[] args.
Either select "apples" at the bottom of the menu you posted and run it, or right-click on the Java file and make it the default thing to run for this project. Or launch it by right-clicking on the file and selecting "run".