We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,313 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Is It Possible To Run Code From a .txt File?

I've done some searching, and it seems a few people have asked similar questions, but none of them were quite what I was looking for.

I need to be able to read lines of code from a .txt file, and then execute them in Java.

Or, if that's impossible, would there be a way to convert that text file into another file type, and then run the code from that?

The code in this text file is going to be user created with another program, so I want to keep this as simple as possible for them... if it's even possible. Also, I'm fairly new to programming, especially java, so please try to make it simple for me too. Haha

6
Contributors
37
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
38
Views
Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I need to be able to read lines of code from a .txt file, and then execute them in Java.

Yes, you could use classes that implement the JavaCompiler interface to compile a java program within your running java program and have the class files created available for using in your program.

It's not simple.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

What "other program" is generating the code? Why the need for two programs?

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

What "other program" is generating the code? Why the need for two programs?

It's a mod for Minecraft. My program allows you to draw the building outside of the game, then save the code for that building into a text file, and then Minecraft will run the code from that text file to create your building in the game.

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If Minecraft is executing the code, then all your program needs to do is write the correct instructions to a text file and send it along. Right?

Why would you need to execute it?

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

Sorry im no help, but if you do find a way to do this, message me :D

timmy568
Deleted Member

If Minecraft is executing the code, then all your program needs to do is write the correct instructions to a text file and send it along. Right?

Why would you need to execute it?

I may be using the wrong terminology I guess.

I just need to place the code from the text file into minecraft, and then then let minecraft do it's thing like normal.

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Then you need to read up on the APIs and tools Minecraft (may) provide for such things. This isn't really a Java question.

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

Minecraft modding is done in Java, so I figured it'd just be a regular Java thing.

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Java is a general programming language. Minecraft is an application written in Java and any interfaces available to it would be those created by the writers or other modders.

You need to hit the Minecraft wiki/forums/docs etc for more specific information.

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

...good way to make a virus or malware

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

you can indeed call the Java compiler API on the fly from Java code. That's how JSP engines (for example) work to provide runtime compilation of generated Java source into classfiles.

jwenting
duckman
Team Colleague
8,522 posts since Nov 2004
Reputation Points: 1,656
Solved Threads: 345
Skill Endorsements: 19

Okay, I figured it, but I've run into one last problem that I haven't been able to figure out.

So, what my program does now, is write everything it needs into a .java file, run a batch file to compile the .java file into a .class file, and put it straight into the minecraft.jar, all with one button.

However, when I compile it, I get a ton of errors, because it has minecraft code, but is being compiled separately from minecraft. So, from what I can figure out, I need to set the classpath to use minecraft.jar while compiling... but this is where I get lost. Like I said, I'm just now learning java, so I don't really understand the javac terminology and stuff. =/ (My program is written in python.)

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

need to set the classpath to use minecraft.jar while compiling.

Read the API doc for the javac command.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I did, that's how I figured out that this is what I need to do. I've tried several different things based on what I read there, but none of them work. It always says that the java file I made doesn't exist, when it definitely does.

Like I said, I'm not actually coding in java, I just have to do this because my program works with minecraft... so I don't really know what I'm doing.

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It always says that the java file I made doesn't exist

If you get error messages, you need to post the full text of the messages if you want any help with them. You also need to show the command you entered and the contents of the folder where the command was entered.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Well, when I just write javac myfile.java

It gives me this error:

LLBLOCK1.java:3: error: cannot find symbol
public class LLBLOCK1 extends Block
                              ^
  symbol: class Block
LLBLOCK1.java:5: error: cannot find symbol
    private World worldObj;
            ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                      ^
  symbol:   class World
  location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
        public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
                                                                        ^
  symbol:   class EntityPlayer
  location: class LLBLOCK1
LLBLOCK1.java:9: error: cannot find symbol
        super(i, j, Material.wood);
                    ^
  symbol:   variable Material
  location: class LLBLOCK1
LLBLOCK1.java:14: error: package Block does not exist
world.setBlockWithNotify(i + 0, j + 0, k + 0, Block.stone.blockID);
                                                   ^
6 errors

Which I think is because it's being compiled away from the minecraft.jar, and I honestly don't know what to write when specifying the path. I'm not going to know where this program is going to be installed, so I need to write it in a way that will work with everyone's computer.

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The "cannot find symbol" error says the compiler can not find the definition for that class named in the error message.
Where are those classes defined?
You need to put them on the classpath for the javac command so it can find them.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I'm assuming they're in the minecraft.jar (which, on my computer, is located at: C:\Users\Jonathan\AppData\Roaming\.minecraft\bin\minecraft.jar). So how do I set the classpath for this?

Lemony Lime
Light Poster
25 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Read the API doc for the javac command to see how to set the classpath for it.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1178 seconds using 2.83MB