I have a Java class which uses a .jar file.
Every time that I want to compile this class, I have to do something like javac -cp ".:myJar.jar" myClass.java and every time that I want to execute it, I have to do the similar thing but with java instead of javac.
Is there a way to avoid doing this?
I know that I could put this jar file into my class path but I don't want to do that. I don't neither want to do a maven projet.
There is nothing preventing you from using an IDE, taking advantage of all its super useful features when developing the program, and then submit just the source code and associated jars to the professor.
Advantages of using IDE that your professor don't need:
Syntax color-coded editors with auto-complete.
Built-in display of javadoc, so you know what all the built-in Java methods do, and how they work.
Instant high-lighting of syntax errors.
Never having to compile the code, since IDE always keeps the code compiled.
Easy single-button execution of the program when you want to run it.
Debugger. Very important feature when your code is not working as you intended.
... more ...
Related
I'm doing some basic java homework for a class on my new laptop - issue is, I can't seem to get the program to compile and run from my batch file using the directions the instructor gave me.
I've set the Path variable to my JDK inside the Environment Variables settings.
My program is a simple shipping program to keep track of shipment information - I have the program working flawlessly in NetBeans (which our instructor advised us to use for developing the code), but he's going to be testing them using batch files, so we're also advised to test them on our systems with one we create prior to turning them in - pretty straightforward.
Issue is, I cannot seem to get this to work. I've never done it before, but I've used .bat files to compile and run C++ programs, as well as using makefiles on a unix system, so I feel like I'm absolutely stupid for not figuring this out on my own, but none of my searches have returned any fruitful solutions that help at all.
My program consists of 3 .java files:
Shipment.java - an interface that contains abstracted methods that are implemented in the ShipmentHW1 class
ShipmentHW1.java - a class that implements the abstracted methods from Shipment and has constructors, etc to create a usable object
TestShipment.java - the main class of this program, which utilizes and creates ShipmentHW1 objects based on preset parameters. This is super duper basic stuff here, and again, it runs perfectly fine inside the NetBeans IDE.
The instructions given to us state to have the batch file inside the package directory (which in this case I've set aside a seperate folder on my desktop titled "shipping", which is the package name - shouldn't be any issues there), where the 3 .java files are located as well.
They say if you don't need to explicitly list the path to the JDK, then you can simply have
javac TestShipment.java
java TestShipment.java
pause
Afterwards I get errors talking about how it "cannot find symbol Shipment s = new ShipmentHW1();"
I've tried adding imports, but since they're in the same package it shouldn't even be an issue.
Directory path is
C:\Users\X\Desktop\shipping
All 7 files are contained within:
TestShipment.java
TestShipment.class
Shipment.java
Shipment.class
ShipmentHW1.java
ShipmentHW1.class
doHW1.bat
Does anyone have any idea? I can provide more information if I've been too vague
Also, I'm on Windows 8 if that makes any difference
Solved
Batch file now reads
javac TestShipment.java Shipment.java ShipmentHW1.java
cd ..
java shipment.TestShipment
pause
and it works like a charm. Anyone have any ideas why I had to call the package.class instead of just compiling it regularly?
Try doing
javac TestShipment.java
java TestShipment
pause
Without seeing the contents of TestShipment.java, I'll assume you have some dependency on the Shipment and ShipmentHW1 classes. As such, when you execute a program that uses the TestShipment class, you need to have the .class files for each of the three (and any other dependencies).
So you will have to compile Shipment.java and ShipmentHW1.java as well before running your java command. If they are in the same package, you're good, if not, you will have to specify an appropriate value for the -cp option.
When running java with a class name, you need to specify the fully qualified class name.
If your .java files are declared to be in the 'shipping' package, then you probably need to be running java from the parent directory of 'shipping', e.g.
cd <path>/shipping
javac TestShipment.java
cd ..
java shipping/TestShipment
I'm trying to compile a whole java project dynamically using the Compiler API. My initial thoughts of achieving this is to first know how to compile it in one-line using command line, then apply the same principle/parameters on the compiler object. Problem is, I never did, err, compiled using CLI. (Disadvantage of using an IDE? Haha)
So, am I on the right track? Moreover, can the project compilation achieved in one line execution? I'm having a hard time figuring this out because of the fact that it's a project, it contains packages galore.
So you either want to learn javac or Java Compiler API?
If you want CLI compilation look at javac (Linux | windows).
Alternatively for API, then programmatic use of Java's compiler API will definitely require more that one line, this will get you the compiler:
JavaCompilercompiler =ToolProvider.getSystemJavaCompiler();
Then you'll still need to load classes, write out byte code, and possibly package as a JAR.
You should use StandardJavaFileManager as you've probably many classes to manage, there's an example in the top of the JavaCompiler javadoc, but search for StandardJavaFileManager+JavaCompiler+example to find clearer examples like this blog post.
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
I have a scenario where I have to generate Java code when my application is running.
Can compile and run than code from my application( as a separate application).
Is it possible to compile and then use it from the same application. Cant think of any possibility
Check out Create dynamic applications with javax.tools. Second time I've referenced this today--I swear I don't work for them.
You can use an instance of JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Follow the link for the an example on how to use it.
using the ProcessBuilder or Runtime.exec() you can run any commandline app from your java code
this includes the javac compiler and java as a separate process
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.