locate imports definition in java - java

I'm a newbie in java. Is there any way to find the required imports(or missing imports) in java programming?. I know that in modern editor like Eclipse and Netbeans. But we have to do java in notepad for examination, that's why.
For example, When I use Jframe, I want to know which is the required imports. Is it possible to locate 'import's definition in a directory or file?

Run the compiler javac which tells you the types that cannot be resolved (this imports are missing).
Then you can use the javadoc from the API. In the left lower corner there is a complete list of classes in the API. Search the class and click on it then you know in which package it is.

Related

GLD Green Light Simulator not compiling

I found a traffic light simulator called Green Light District
the file I download included and explanation of how to build the project
and it says the following
Extract the archive to a certain directory,
In that dir do javac gld/*.java
In that dir do java gld.GLDSim
I know * means all but...
first I want to know am I able to compile something like *.java
this is a link to the simulator , if some one can tell how to compile it , it would be really nice
http://sourceforge.net/projects/stoplicht/
The issue occurs due to the fact that "enum" became reserved word in Java 1.5+, so it can no longer be used as a name for variables/methods/classes, but GLD uses enum as a name for some variables.
Once the name is changed - project will compile.
Alternatively one can try to compile it with compiler setting source version 1.4 - this should probably also work, but I suggest to simply rename all variables named enum.
Basically, all the code is in the folder called gld. You need to compile all the .java files inside it. After that, you run the main class gld.GLDSim.
Had the same problem.
You should use Java 1.4, this simulator is quite old, hence uses an old version of Java and some preserved words in more advanced Java versions.

Imported jar files creates empty packages

I am importing a Jar file "com.ibm.mq.jar" into my workspace(Eclipse IDE).
While importing, a screen came where I could see all the classes in the Jar file.
After I imported it into the work space, I was able to import the package and following statement didn't give any error.
import com.ibm.mq.*;
But, in code I am not able to use any of the classes which were there in the package.
Like, "MQC" is a class in the package, but in code it doesn't reflect("MQC cannot be resolved as a type" error comes if I try to use it).
This jar file actually contains Websphere MQ API classes.
Can anyone advise, what am I missing.
If you're using MQ 7, check its documentation here. There was some stuff going on about deprecation of com.ibm.mq.mqc and, depending on the version you use, that class was replaced by com.ibm.mq.constants.MQConstants. Like this one, there are other cases.
In fact com.ibm.mq only contains the exception MQException, so you won't find any classes there. I suggest you check the version you're using and dig a little deeper into the docs, as a first step.

Import Error in Java Using Ant but not Eclipse

I can compile my program just fine if I specify sublibraries of com.itextpdf.*, but for some reason, my compile fails if I simply do import com.itextpdf.*. But, like I said, if I specify the sublibraries I'm using, such as
import com.itextpdf.text.Font.*;
import com.itextpdf.text.pdf.*;
Everything compiles just fine. In the case where it fails, the error I'm getting is "package com.itextpdf does not exist", but I know it does. I include it properly in the build path, and Eclipse doesn't give any compile errors in any case. The error only comes up when I try to build with Ant.
At this point, I don't mind specifying the sub libraries I'm going to use to make sure I get a build. But I am curious, why wouldn't Ant allow this?
You, and Eclipse, think of java namespaces as a hierarchy. However, in the Java specification, they are not. There is no relationship between "com.itextpdf.text" and "com.itextpdf.text.Font". They are different strings, end of story.
As a matter of style, you may want to set up Eclipse to automatically organize your imports and explicitly specify every class you use in the import section of your class. This avoids ambiguity when "com.itextpdf.text.Font.String" and "com.itextpdf.text.pdf.String" both exist, but you only want to reference one of them. This style would be incredibly tedious and inconvenient if you were programming in emacs or vi, but with Eclipse it is automatic and invisible, unless you scroll to the top of your file.

How to create a method out of the text file?

I have a text (.txt) file that contains Java code! I want to create a method that includes this Java code and then call that method through the program.
Can anybody suggest a way to do this?
let consider this example what it does actually load the source code, compile and execute the java code by simpler program by using JavaCompiler API.
Use the JavaCompiler. It can compile code from a String, so I'm sure it could handle code from a text file.
Do you think instead of putting it in the main method I can put it in for example test method and call method like this?
Put it wherever you like. E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
this program need tools.jar but jre 7 doesnt have this!!
Did you try reading the documentation that is provided for the STBC? Notably:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
Or shorter, no JRE will have a JavaCompiler, only JDKs have them.
Change the .txt file to a .java file,
add it to your java project
Compile the code
Execute the methods
Load the file in through standard java IO and then have Groovy evaluate it for you:
http://groovy.codehaus.org/Embedding+Groovy
it's something like quine:
http://www.nyx.org/%7Egthompso/quine.htm

Is there a way to run short bits of Java code without compiling?

Is there a way to run or simulate running Java statements (kind of like IDLE - the Python GUI) without compiling and running the executable? I want to quickly test statements to see if they work. Thanks.
Yep, you can use Eclipse, create a single project, and create a Scrapbook Page in that project.
You can also specify import statements: http://www.informit.com/articles/article.aspx?p=31789&seqNum=3
Scrapbook pages get their classpath
from the containing project's build
path. If in a scrapbook page you want
to reference a Java element that is
not on the build path of the
containing Java project, you need to
add to the Java project's build path.
Scrapbook pages also allow you to
specify import statements. You do this
by selecting Set Imports from the
context menu of a scrapbook page or
Set Import Declarations for Running
Code from the toolbar. You need to set
import statements for references to
Java declarations in your projects.
This is a common oversight. If the
type or package you are attempting to
import is not listed in the Add
dialog, it means you need to add it to
the build path of the project
containing the scrapbook page. If you
are referencing an element that has
multiple declarations, you will need
to add an import statement to uniquely
identify the element.
Edit: Got another solution too: http://ideone.com. It's an online IDE and debugging tool. You can see an example here: http://ideone.com/98sA8, but it looks like you have to set up a bit more than on a scrapbook page.
Edit 2:
Nowadays in Java 11, if it's a simple app in a single file you can run it directly from the java command (on the command line) which will handle all the compilation for you behind the scenes:
java HelloWorld.java
This is useful for students, as they can get started with Java without learning all of the javac compilation routine.
As of Java 11 (JEP 330) it is now possible to run Java files directly with the java tool:
java Factorial.java 3 4 5
is informally equivalent to
javac -d <memory> Factorial.java
java -cp <memory> Factorial 3 4 5
Java also added support for "shebang" files.
For more details see:
http://openjdk.java.net/jeps/330
Using Eclipse, you can create a Scrapbook page which will allow you to do exactly this.
Caveats:
You need to use full binary class names for anything outside of java.lang
You need to select the code (standard text selection) that you want to run
There are three different methods for running -- Inspect, Display, and Run. Inspect and Display are virtually the same, showing you the result of the last statement in your code (so you don't need to print it or anything), Run runs the selected code and dumps any output to the console view.
Never used it, but BeanShell seems to do what you want
You should be able to use Beanshell to do this:
http://www.beanshell.org/download.html
Your other alternative, if you're using Eclipse, is to make use of the scrapbook functionality:
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.user/tasks/task-create_scrapbook_page.htm
You can accomplish this with Groovy and the Groovy Console, with the caveat that you'd need to know how to express whatever you are trying to express in Java in the Groovy language:
you might want to checkout janino http://docs.codehaus.org/display/JANINO/Home also ..
JGrasp is the best solution. There is a thing called interactions, that's perfectly fine.
Use JShell, which is included by default starting from JDK 9. It is command-line based Read Eval Print Loop (REPL), where you can enter Java code, and get the results immediately.

Categories